Skip to content

Commit 4ea7b68

Browse files
committed
add download content feature over cgi-ctl
1 parent 0c3c812 commit 4ea7b68

8 files changed

Lines changed: 200 additions & 14 deletions

File tree

assets/bindata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:e07b096a9b6b906b5e7e8e7c85b5aa2ec67c5de2e7788cf88a269ec1558b0392
3-
size 4848983
2+
oid sha256:4ef0fa32de0d86efc582ef952a1657da50d6371ffb66ef27ecb876bf83f5af68
3+
size 4851447

cmd/cgi-ctl/cmd_download.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

cmd/cgi-ctl/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"github.com/jessevdk/go-flags"
5+
"github.com/reddec/trusted-cgi/cmd/internal"
56
"github.com/reddec/trusted-cgi/types"
67
"io/ioutil"
78
"os"
@@ -16,6 +17,7 @@ type Config struct {
1617
Init struct {
1718
Bare Bare `command:"bare" description:"create bare template"`
1819
} `command:"init" description:"initialize function in a current directory"`
20+
Download download `command:"download" description:"download lambda content to the local tarball or stdout"`
1921
}
2022

2123
func main() {
@@ -70,7 +72,9 @@ func (b Bare) Execute(args []string) error {
7072
}
7173

7274
if b.Git {
73-
return exec.Command("git", "init").Run()
75+
gctx, closer := internal.SignalContext()
76+
defer closer()
77+
return exec.CommandContext(gctx, "git", "init").Run()
7478
}
7579
return nil
7680
}

cmd/internal/utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package internal
2+
3+
import (
4+
"context"
5+
"os"
6+
"os/signal"
7+
)
8+
9+
func SignalContext() (context.Context, func()) {
10+
gctx, closer := context.WithCancel(context.Background())
11+
go func() {
12+
c := make(chan os.Signal, 2)
13+
signal.Notify(c, os.Kill, os.Interrupt)
14+
for range c {
15+
closer()
16+
break
17+
}
18+
}()
19+
return gctx, closer
20+
}

cmd/trusted-cgi/main.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"github.com/jessevdk/go-flags"
66
"github.com/reddec/trusted-cgi/api/services"
77
"github.com/reddec/trusted-cgi/application"
8+
"github.com/reddec/trusted-cgi/cmd/internal"
89
"github.com/reddec/trusted-cgi/server"
910
"github.com/reddec/trusted-cgi/stats/impl/memlog"
1011
"log"
1112
"net/http"
1213
"os"
13-
"os/signal"
1414
"time"
1515
)
1616

@@ -70,15 +70,7 @@ func main() {
7070
os.Exit(1)
7171
}
7272

73-
gctx, closer := context.WithCancel(context.Background())
74-
go func() {
75-
c := make(chan os.Signal, 2)
76-
signal.Notify(c, os.Kill, os.Interrupt)
77-
for range c {
78-
closer()
79-
break
80-
}
81-
}()
73+
gctx, closer := internal.SignalContext()
8274
defer closer()
8375
err = run(gctx, config)
8476
if err != nil {

docs/_layouts/default.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ <h3>Features & docs</h3>
5757
</ul>
5858
<ul>
5959
<li><a href="/static">Static files</a></li>
60+
<li><a href="/cgi_ctl">CGI-CTL</a></li>
6061
</ul>
6162
<h3>API</h3>
6263
<ul>

docs/cgi_ctl.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# CGI-CTL command help
2+
3+
4+
`cgi-ctl` command aims to be a tool for helping developers interact with the platform without web UI.
5+
6+
`cgi-ctl` includes into distribution starting from version 0.3.1 and could be obtained independently
7+
via source code or pre-built binary ([see installation](installation.md))
8+
9+
## init bare
10+
11+
From `0.3.0`
12+
13+
Initialize a basic lambda in a current directory.
14+
15+
```
16+
Usage:
17+
cgi-ctl [OPTIONS] init bare [bare-OPTIONS]
18+
19+
Help Options:
20+
-h, --help Show this help message
21+
22+
[bare command options]
23+
--git Enable Git [$GIT]
24+
-d, --description= Description (default: Bare project) [$DESCRIPTION]
25+
-P, --private Mark as private [$PRIVATE]
26+
-t, --time-limit= Time limit for execution (default: 10s) [$TIME_LIMIT]
27+
-p, --max-payload= Maximum payload (default: 8192) [$MAX_PAYLOAD]
28+
29+
```
30+
31+
## download
32+
33+
From `0.3.1`
34+
35+
Download the lambda content from a remote instance of `trusted-cgi`.
36+
37+
```
38+
Usage:
39+
cgi-ctl [OPTIONS] download [download-OPTIONS]
40+
41+
Help Options:
42+
-h, --help Show this help message
43+
44+
[download command options]
45+
-l, --login= Login name (default: admin) [$LOGIN]
46+
-p, --password= Password (default: admin) [$PASSWORD]
47+
-P, --ask-pass Get password from stdin [$ASK_PASS]
48+
-u, --url= Trusted-CGI endpoint (default: http://127.0.0.1:3434/) [$URL]
49+
-i, --uid= Lambda UID [$UID]
50+
-o, --output= Output data (- means stdout, empty means as UID) [$OUTPUT]
51+
```
52+
53+
**Example 1** (from local dev instance, lambda `e0ed902f-4a9c-4c29-870d-f343f330b6ab`):
54+
55+
```
56+
cgi-ctl download -i e0ed902f-4a9c-4c29-870d-f343f330b6ab
57+
```
58+
59+
will create `e0ed902f-4a9c-4c29-870d-f343f330b6ab.tar.gz` archive.
60+
61+
62+
**Example 2** (from the remote instance, same lambda):
63+
64+
```
65+
cgi-ctl download -i e0ed902f-4a9c-4c29-870d-f343f330b6ab --url https://example.com/ -P
66+
```
67+
68+
will ask password for `admin` user and then download archive
69+
70+
**Example 3** (from the remote instance, same lambda):
71+
72+
```
73+
cgi-ctl download -i e0ed902f-4a9c-4c29-870d-f343f330b6ab --url https://example.com/ -P -o - | tar zxf -
74+
```
75+
76+
will ask password for `admin` user, download archive and unpack to current directory

0 commit comments

Comments
 (0)