diff --git a/sftp.go b/sftp.go index 589b406..55d9216 100644 --- a/sftp.go +++ b/sftp.go @@ -8,25 +8,20 @@ import ( "golang.org/x/crypto/ssh" ) -var ( - dstPath string = "/home/wyatt/" - srcPath string = "/var/log/" - filename string = "dpkg.log" -) - -func (s SSHConnection) getFile(client *ssh.Client) { - sftp, err := sftp.NewClient(client) - //var srcFile []*os.File - //var dstFile []*os.File - - if err != nil { - fmt.Errorf("Error") - } - - defer sftp.Close() +func (s SSHConnection) getFile(client *ssh.Client) []*os.File { + homedir, _ := os.UserHomeDir() + var dstFiles []*os.File for _, j := range s.Logs { - srcFile, err := sftp.Open(srcPath + j) + sftp, err := sftp.NewClient(client) + + if err != nil { + fmt.Errorf("Error") + } + + defer sftp.Close() + + srcFile, err := sftp.Open(j) if err != nil { fmt.Errorf("Error") @@ -34,7 +29,9 @@ func (s SSHConnection) getFile(client *ssh.Client) { defer srcFile.Close() - dstFile, err := os.Create(dstPath + j) + h := slashSeperator(j) + + dstFile, err := os.Create(homedir + "/" + h) if err != nil { fmt.Errorf("Error") @@ -42,6 +39,9 @@ func (s SSHConnection) getFile(client *ssh.Client) { defer dstFile.Close() srcFile.WriteTo(dstFile) + + dstFiles = append(dstFiles, dstFile) } + return dstFiles } diff --git a/ssh.go b/ssh.go index ca0289d..d948b5f 100644 --- a/ssh.go +++ b/ssh.go @@ -94,23 +94,19 @@ func (s SSHConnection) openSession(client SSHClients) SSHSessions { func (s SSHConnection) executeSFTP(execute SSHClients) SSHSFTP { // execute order 66 lol sftp := SSHSFTP{} - homedir := os.Getenv("HOME") + homedir, _ := os.UserHomeDir() for _, j := range execute { - // TODO: this is just a placeholder, change to the actual tarring executable - s.getFile(j) + slashed := s.getFile(j) + for _, k := range slashed { + name := osfileToSting(k) + err := gzipit(homedir+"/"+name, ".") - err := gzipit(homedir+filename, ".") - - if err != nil { - fmt.Errorf("Cannot gzip file(s)", err) + if err != nil { + fmt.Errorf("Cannot gzip file(s)", err) + } } - } return sftp } - -func (s SSHConnection) gzipItUp() { - // TODO: placeholder function?? -} diff --git a/string.go b/string.go index aa937e3..ff3c6c4 100644 --- a/string.go +++ b/string.go @@ -1,7 +1,31 @@ package main -import "time" +import ( + "fmt" + "os" + "path/filepath" + "time" +) func timeToString(currentTime time.Time) string { return currentTime.String() } + +func osfileToSting(currentOsFile *os.File) string { + file, err := os.Open(currentOsFile.Name()) + + fileinfo, err := file.Stat() + + if err != nil { + fmt.Printf("Heyo, there's no file here!\n") + } + + name := fileinfo.Name() + return name +} + +func slashSeperator(unslashed string) string { + s := unslashed + _, file := filepath.Split(s) + return file +}