|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/reddec/trusted-cgi/api" |
| 7 | + "github.com/reddec/trusted-cgi/api/client" |
| 8 | + "github.com/reddec/trusted-cgi/cmd/internal" |
| 9 | + "golang.org/x/crypto/ssh/terminal" |
| 10 | + "io" |
| 11 | + "log" |
| 12 | + "os" |
| 13 | + "strings" |
| 14 | + "syscall" |
| 15 | +) |
| 16 | + |
| 17 | +type remoteLink struct { |
| 18 | + Login string `short:"l" long:"login" env:"LOGIN" description:"Login name" default:"admin"` |
| 19 | + Password string `short:"p" long:"password" env:"PASSWORD" description:"Password" default:"admin"` |
| 20 | + AskPass bool `short:"P" long:"ask-pass" env:"ASK_PASS" description:"Get password from stdin"` |
| 21 | + URL string `short:"u" long:"url" env:"URL" description:"Trusted-CGI endpoint" default:"http://127.0.0.1:3434/"` |
| 22 | +} |
| 23 | + |
| 24 | +func (rl *remoteLink) Users() *client.UserAPIClient { |
| 25 | + return &client.UserAPIClient{BaseURL: rl.URL + "u/"} |
| 26 | +} |
| 27 | + |
| 28 | +func (rl *remoteLink) Lambdas() *client.LambdaAPIClient { |
| 29 | + return &client.LambdaAPIClient{BaseURL: rl.URL + "u/"} |
| 30 | +} |
| 31 | + |
| 32 | +func (rl *remoteLink) Token(ctx context.Context) (*api.Token, error) { |
| 33 | + if rl.AskPass { |
| 34 | + _, _ = fmt.Fprintf(os.Stderr, "Enter Password: ") |
| 35 | + bytePassword, err := terminal.ReadPassword(syscall.Stdin) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + rl.Password = strings.TrimSpace(string(bytePassword)) |
| 40 | + } |
| 41 | + return rl.Users().Login(ctx, rl.Login, rl.Password) |
| 42 | +} |
| 43 | + |
| 44 | +type download struct { |
| 45 | + remoteLink |
| 46 | + UID string `short:"i" long:"uid" env:"UID" description:"Lambda UID" required:"yes"` |
| 47 | + Output string `short:"o" long:"output" env:"OUTPUT" description:"Output data (- means stdout, empty means as UID)" default:""` |
| 48 | +} |
| 49 | + |
| 50 | +func (cmd *download) Execute(args []string) error { |
| 51 | + ctx, closer := internal.SignalContext() |
| 52 | + defer closer() |
| 53 | + log.SetOutput(os.Stderr) |
| 54 | + log.Println("login...") |
| 55 | + token, err := cmd.Token(ctx) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("login: %w", err) |
| 58 | + } |
| 59 | + log.Println("download...") |
| 60 | + tarball, err := cmd.Lambdas().Download(ctx, token, cmd.UID) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("download: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + var out io.Writer |
| 66 | + if cmd.Output == "" { |
| 67 | + cmd.Output = cmd.UID + ".tar.gz" |
| 68 | + } |
| 69 | + if cmd.Output == "-" { |
| 70 | + out = os.Stdout |
| 71 | + } else { |
| 72 | + log.Println("saving to", cmd.Output, "...") |
| 73 | + f, err := os.Create(cmd.Output) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("create destination file: %w", err) |
| 76 | + } |
| 77 | + defer f.Close() |
| 78 | + out = f |
| 79 | + } |
| 80 | + var w int |
| 81 | + for w < len(tarball) { |
| 82 | + n, err := out.Write(tarball[w:]) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + if n <= 0 { |
| 87 | + break |
| 88 | + } |
| 89 | + w += n |
| 90 | + } |
| 91 | + log.Println("done") |
| 92 | + return nil |
| 93 | +} |
0 commit comments