|
| 1 | +// +build go1.7 |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "runtime" |
| 11 | + |
| 12 | + "github.com/fatih/color" |
| 13 | + "github.com/jawher/mow.cli" |
| 14 | + "github.com/k-takata/go-iscygpty" |
| 15 | + "github.com/mattn/go-isatty" |
| 16 | + "github.com/raoulh/go-progress" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + CharStar = "\u2737" |
| 21 | + CharAbort = "\u2718" |
| 22 | + CharCheck = "\u2714" |
| 23 | + CharWarning = "\u26A0" |
| 24 | + CharArrow = "\u2012\u25b6" |
| 25 | + CharVertLine = "\u2502" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + blue = color.New(color.FgBlue).SprintFunc() |
| 30 | + errorRed = color.New(color.FgRed).SprintFunc() |
| 31 | + errorBgRed = color.New(color.BgRed, color.FgBlack).SprintFunc() |
| 32 | + green = color.New(color.FgGreen).SprintFunc() |
| 33 | + cyan = color.New(color.FgCyan).SprintFunc() |
| 34 | + bgCyan = color.New(color.FgWhite).SprintFunc() |
| 35 | +) |
| 36 | + |
| 37 | +var ( |
| 38 | + mcUrl *string |
| 39 | + debugOpt *bool |
| 40 | + |
| 41 | + optContext *string |
| 42 | + optLogin *string |
| 43 | + optPass *string |
| 44 | + optDesc *string |
| 45 | + optPrintDesc *bool |
| 46 | + optFilename *string |
| 47 | + |
| 48 | + isTerminal bool |
| 49 | + progressBar *progress.ProgressBar |
| 50 | +) |
| 51 | + |
| 52 | +func exit(err error, exit int) { |
| 53 | + fmt.Fprintln(os.Stderr, errorRed(CharAbort), err) |
| 54 | + cli.Exit(exit) |
| 55 | +} |
| 56 | + |
| 57 | +func checkLog() { |
| 58 | + if !*debugOpt { |
| 59 | + //completely disable debug output |
| 60 | + log.SetFlags(0) |
| 61 | + log.SetOutput(ioutil.Discard) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func addDefaultArgs(cmd *cli.Cmd) { |
| 66 | + mcUrl = cmd.StringOpt("m moolticute_url", MOOLTICUTE_DAEMON_URL, "Use a different url for connecting to moolticute") |
| 67 | + debugOpt = cmd.BoolOpt("debug", false, "Add debug log to stdout") |
| 68 | +} |
| 69 | + |
| 70 | +func main() { |
| 71 | + runtime.GOMAXPROCS(runtime.NumCPU()) |
| 72 | + |
| 73 | + // fix for cygwin terminal |
| 74 | + if iscygpty.IsCygwinPty(os.Stdout.Fd()) || isatty.IsTerminal(os.Stdout.Fd()) { |
| 75 | + color.NoColor = false |
| 76 | + isTerminal = true |
| 77 | + } |
| 78 | + |
| 79 | + app := cli.App("moolticute-cli", "Command line tool to interact with a mooltipass device through a moolticute daemon") |
| 80 | + |
| 81 | + app.Command("login", "Manage credentials stored in the device", func(cmd *cli.Cmd) { |
| 82 | + cmd.Command("get", "Get a password for given context", func(cmd *cli.Cmd) { |
| 83 | + optContext = cmd.StringArg("CONTEXT", "", "Context to work on") |
| 84 | + optLogin = cmd.StringArg("LOGIN", "", "Login to use") |
| 85 | + addDefaultArgs(cmd) |
| 86 | + optPrintDesc = cmd.BoolOpt("d description", false, "Output service description instead of password") |
| 87 | + cmd.Spec = "CONTEXT LOGIN [OPTIONS]" |
| 88 | + |
| 89 | + cmd.Action = func() { |
| 90 | + checkLog() |
| 91 | + |
| 92 | + if err := processLoginCmd("get", *optContext, *optLogin, "", "", *optPrintDesc); err != nil { |
| 93 | + exit(err, 1) |
| 94 | + } |
| 95 | + } |
| 96 | + }) |
| 97 | + cmd.Command("set", "Add or update a context", func(cmd *cli.Cmd) { |
| 98 | + optContext = cmd.StringArg("CONTEXT", "", "Context to work on") |
| 99 | + optLogin = cmd.StringArg("LOGIN", "", "Login to set") |
| 100 | + optPass = cmd.StringArg("PASS", "", "Password to set") |
| 101 | + optDesc = cmd.StringArg("DESC", "", "Description to set") |
| 102 | + addDefaultArgs(cmd) |
| 103 | + cmd.Spec = "CONTEXT LOGIN PASS [DESC] [OPTIONS]" |
| 104 | + |
| 105 | + cmd.Action = func() { |
| 106 | + checkLog() |
| 107 | + |
| 108 | + if err := processLoginCmd("set", *optContext, *optLogin, *optPass, *optDesc, false); err != nil { |
| 109 | + exit(err, 1) |
| 110 | + } |
| 111 | + } |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + app.Command("data", "Import & export small files stored in the device", func(cmd *cli.Cmd) { |
| 116 | + cmd.Command("get", "Retrieve data for given context", func(cmd *cli.Cmd) { |
| 117 | + optContext = cmd.StringArg("CONTEXT", "", "Context to work on") |
| 118 | + addDefaultArgs(cmd) |
| 119 | + cmd.Spec = "CONTEXT [OPTIONS]" |
| 120 | + |
| 121 | + cmd.Action = func() { |
| 122 | + checkLog() |
| 123 | + |
| 124 | + if err := processDataCmd("get", *optContext, "", progressFunc); err != nil { |
| 125 | + exit(err, 1) |
| 126 | + } |
| 127 | + } |
| 128 | + }) |
| 129 | + cmd.Command("set", "Add or update data for given context", func(cmd *cli.Cmd) { |
| 130 | + optContext = cmd.StringArg("CONTEXT", "", "Context to work on") |
| 131 | + optFilename = cmd.StringArg("FILENAME", "", "File to save in the device") |
| 132 | + addDefaultArgs(cmd) |
| 133 | + cmd.Spec = "CONTEXT FILENAME [OPTIONS]" |
| 134 | + |
| 135 | + cmd.Action = func() { |
| 136 | + checkLog() |
| 137 | + |
| 138 | + if err := processDataCmd("set", *optContext, *optFilename, progressFunc); err != nil { |
| 139 | + exit(err, 1) |
| 140 | + } |
| 141 | + } |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + app.Command("parameters", "Get/Set device parameters", func(cmd *cli.Cmd) { |
| 146 | + checkLog() |
| 147 | + }) |
| 148 | + |
| 149 | + if err := app.Run(os.Args); err != nil { |
| 150 | + exit(err, 1) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func progressFunc(total, current int) { |
| 155 | + if !isTerminal { |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + if progressBar == nil { |
| 160 | + progressBar = progress.New(total) |
| 161 | + progressBar.Format = progress.ProgressFormats[2] |
| 162 | + } |
| 163 | + |
| 164 | + progressBar.Set(current) |
| 165 | +} |
0 commit comments