forked from raoulh/mc-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
executable file
·118 lines (95 loc) · 2.12 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"github.com/howeyc/gopass"
)
const (
maxFileSize = 524288
)
func processLoginCmd(subCmd, context, login, pwd, desc string, printDesc bool) (err error) {
m := &MoolticuteMsg{
Data: MsgData{
Service: context,
Login: login,
},
}
if subCmd == "get" {
m.Msg = "get_credential"
} else if subCmd == "set" {
if pwd == "" {
fmt.Printf("Password: ")
p, err := gopass.GetPasswdMasked()
if err != nil {
return err
}
pwd = string(p)
}
m.Msg = "set_credential"
m.Data.Password = pwd
m.Data.Description = desc
}
res, err := McSendQuery(m)
if err != nil {
return err
}
if subCmd == "get" {
if printDesc {
fmt.Println(res.Description)
} else {
fmt.Println(res.Password)
}
} else if subCmd == "set" {
fmt.Println(green(CharCheck), "Done")
}
return
}
func processDataCmd(subCmd, context, filename string, progressFunc ProgressCb) (err error) {
m := &MoolticuteMsg{
Data: MsgData{
Service: context,
},
}
if subCmd == "get" {
m.Msg = "get_data_node"
} else if subCmd == "set" {
m.Msg = "set_data_node"
//open file and encode to base64
finfo, err := os.Stat(filename)
if err != nil {
return fmt.Errorf("Failed to get file info: %v", err)
}
if finfo.Size() > maxFileSize {
return fmt.Errorf("File is too big for beeing saved into the mooltipass :(")
}
b, err := ioutil.ReadFile(filename)
if err != nil {
return fmt.Errorf("Failed to read file: %v", err)
}
m.Data.NodeData = base64.StdEncoding.EncodeToString(b)
}
res, err := McSendQueryProgress(m, progressFunc)
if err != nil {
return err
}
if subCmd == "get" {
//decode the base64
bdec, err := base64.StdEncoding.DecodeString(res.NodeData)
if err != nil {
err = fmt.Errorf("Failed to base64 decode data:", err)
return err
}
b := bytes.NewBuffer(bdec)
b.WriteTo(os.Stdout)
} else if subCmd == "set" {
fmt.Println(green(CharCheck), "Done")
}
return
}
func processParameterCmd(subCmd, parameter string, value string) (err error) {
fmt.Println(errorRed(CharAbort), "Not implemented yet")
return (nil)
}