diff --git a/.gitignore b/.gitignore index 99378f6..2afd239 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ private/* tmp/* +goback diff --git a/client/client.go b/client/client.go index 05e7b4e..3515368 100644 --- a/client/client.go +++ b/client/client.go @@ -10,7 +10,7 @@ import ( "golang.org/x/crypto/ssh" ) -func connectToServer(user, password, host string) (*ssh.Client, error) { +func ConnectToServer(user, password, host string) (*ssh.Client, error) { sshConfig := &ssh.ClientConfig{ User: user, Auth: []ssh.AuthMethod{ssh.Password(password)}, @@ -23,13 +23,7 @@ func connectToServer(user, password, host string) (*ssh.Client, error) { return client, nil } -func Upload(f string) { - client, err := connectToServer("demo", "password", "127.0.0.1:2022") - if err != nil { - log.Fatalf("Failed to connect to server: %v", err) - } - defer client.Close() - +func Upload(client *ssh.Client, f string) { sftpClient, err := sftp.NewClient(client) if err != nil { log.Fatalf("Failed to create SFTP client: %v", err) diff --git a/cmd/client.go b/cmd/client.go new file mode 100644 index 0000000..1b28ea4 --- /dev/null +++ b/cmd/client.go @@ -0,0 +1,42 @@ +package cmd + +import ( + "log" + + "github.com/bsach64/goback/client" + "github.com/spf13/cobra" +) + +var ( + clientCmd = &cobra.Command{ + Use: "client -u [user] -p [password] -H [host_addr] -f [filepath]", + Short: "starts a client that connects to [host_addr] and sends [filepath]", + Long: "starts a client that connects to [host_addr] and sends [filepath]", + Run: StartClient, + } + + clientArgs struct { + user string + password string + host string + f string + } +) + +func StartClient(cmd *cobra.Command, args []string) { + c, err := client.ConnectToServer(clientArgs.user, clientArgs.password, clientArgs.host) + if err != nil { + log.Fatalf("Failed to connect to server: %v", err) + } + defer c.Close() + client.Upload(c, clientArgs.f) +} + +func init() { + rootCmd.AddCommand(clientCmd) + clientCmd.Flags().StringVarP(&clientArgs.user, "user", "u", "demo", "username") + clientCmd.Flags().StringVarP(&clientArgs.password, "password", "p", "password", "password") + clientCmd.Flags().StringVarP(&clientArgs.host, "host", "H", "127.0.0.1:2022", "host address") + clientCmd.Flags().StringVarP(&clientArgs.f, "filepath", "f", "", "file path") + clientCmd.MarkFlagRequired("filepath") +} diff --git a/cmd/root.go b/cmd/root.go index acf7351..c0277d8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "os" "github.com/spf13/cobra" @@ -12,11 +11,6 @@ var rootCmd = &cobra.Command{ Use: "goback", Short: "A brief description of your application", Long: `A cli tool to facilitate local backups`, - // Uncomment the following line if your bare application - // has an action associated with it: - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Hello") - }, } func Execute() { diff --git a/cmd/server.go b/cmd/server.go new file mode 100644 index 0000000..77f537b --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "github.com/bsach64/goback/server" + "github.com/spf13/cobra" +) + +var serverCmd = &cobra.Command{ + Use: "server", + Short: "starts a server", + Long: "starts a server", + Run: func(cmd *cobra.Command, args []string) { + server.Listen() + }, +} + +func init() { + rootCmd.AddCommand(serverCmd) +} diff --git a/main.go b/main.go index bd9fdb9..d0efb5c 100644 --- a/main.go +++ b/main.go @@ -1,21 +1,9 @@ package main import ( - // "fmt" - "time" - - "github.com/bsach64/goback/client" - "github.com/bsach64/goback/server" - // "github.com/bsach64/goback/utils" + "github.com/bsach64/goback/cmd" ) func main() { - // f, err := utils.ChunkFile("example.txt") - - go server.Listen() - time.Sleep(2 * time.Second) - client.Upload("example.txt") - + cmd.Execute() } - -// Push this where