Skip to content

Commit

Permalink
Merge pull request #6 from bsach64/cli-options
Browse files Browse the repository at this point in the history
Cli options
  • Loading branch information
Unic-X authored Aug 31, 2024
2 parents aefd169 + 9841425 commit 295cdbd
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
private/*
tmp/*
goback
10 changes: 2 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand All @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions cmd/client.go
Original file line number Diff line number Diff line change
@@ -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")
}
6 changes: 0 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
Expand All @@ -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() {
Expand Down
19 changes: 19 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 2 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 295cdbd

Please sign in to comment.