-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: add
tilt snapshot view
command to view a snapshot from disk (#…
- Loading branch information
Showing
16 changed files
with
1,419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"time" | ||
|
||
"github.com/mattn/go-tty" | ||
"github.com/pkg/browser" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/sync/errgroup" | ||
|
||
"github.com/tilt-dev/tilt/internal/analytics" | ||
engineanalytics "github.com/tilt-dev/tilt/internal/engine/analytics" | ||
"github.com/tilt-dev/tilt/internal/snapshots" | ||
) | ||
|
||
func newSnapshotCmd() *cobra.Command { | ||
result := &cobra.Command{ | ||
Use: "snapshot", | ||
} | ||
|
||
result.AddCommand(newViewCommand()) | ||
|
||
return result | ||
} | ||
|
||
type serveCmd struct { | ||
noOpen bool | ||
} | ||
|
||
func newViewCommand() *cobra.Command { | ||
c := &serveCmd{} | ||
result := &cobra.Command{ | ||
Use: "view <path/to/snapshot.json>", | ||
Short: "Serves the specified snapshot file and optionally opens it in the browser", | ||
Long: "Serves the specified snapshot file and optionally opens it in the browser", | ||
Example: ` | ||
# Run tilt ci and save a snapshot | ||
tilt ci --output-snapshot-on-exit=snapshot.json | ||
# View that snapshot | ||
tilt snapshot view snapshot.json | ||
# Or pipe the snapshot to stdin and specify the snapshot as '-' | ||
curl http://myci.com/path/to/snapshot | tilt snapshot view - | ||
`, | ||
Args: cobra.ExactArgs(1), | ||
Run: c.run, | ||
} | ||
|
||
result.Flags().BoolVar(&c.noOpen, "no-open", false, "Do not automatically open the snapshot in the browser") | ||
|
||
addStartServerFlags(result) | ||
|
||
return result | ||
} | ||
|
||
// blocks until any key is pressed or ctx is canceled | ||
func waitForKey(ctx context.Context) error { | ||
t, err := tty.Open() | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { _ = t.Close() }() | ||
|
||
done := make(chan struct{}) | ||
errCh := make(chan error) | ||
|
||
go func() { | ||
_, err = t.ReadRune() | ||
if err != nil { | ||
errCh <- err | ||
return | ||
} | ||
close(done) | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-done: | ||
return nil | ||
case err := <-errCh: | ||
return err | ||
} | ||
} | ||
|
||
func (c *serveCmd) run(_ *cobra.Command, args []string) { | ||
err := c.serveSnapshot(args[0]) | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "error: %v", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func readSnapshot(snapshotArg string) ([]byte, error) { | ||
var r io.Reader | ||
if snapshotArg == "-" { | ||
r = os.Stdin | ||
} else { | ||
f, err := os.Open(snapshotArg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
r = f | ||
defer func() { _ = f.Close() }() | ||
} | ||
|
||
return ioutil.ReadAll(r) | ||
} | ||
|
||
func (c *serveCmd) serveSnapshot(snapshotPath string) error { | ||
ctx := preCommand(context.Background(), "snapshot view") | ||
a := analytics.Get(ctx) | ||
cmdTags := engineanalytics.CmdTags(map[string]string{}) | ||
a.Incr("cmd.snapshot.view", cmdTags.AsMap()) | ||
defer a.Flush(time.Second) | ||
|
||
url := fmt.Sprintf("http://localhost:%d/snapshot/local", webPortFlag) | ||
|
||
fmt.Printf("Serving snapshot at %s\n", url) | ||
|
||
wg, ctx := errgroup.WithContext(ctx) | ||
wg.Go(func() error { | ||
snapshot, err := readSnapshot(snapshotPath) | ||
if err != nil { | ||
return err | ||
} | ||
return snapshots.Serve(ctx, snapshot, webPortFlag) | ||
}) | ||
|
||
// give the server a little bit of time to spin up | ||
time.Sleep(200 * time.Millisecond) | ||
|
||
if !c.noOpen { | ||
err := browser.OpenURL(url) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
keyPressed := errors.New("pressed key to exit") | ||
wg.Go(func() error { | ||
fmt.Println("Press any key to exit") | ||
err := waitForKey(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
return keyPressed | ||
}) | ||
|
||
err := wg.Wait() | ||
if err != nil && err != keyPressed { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package snapshots | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/tilt-dev/tilt/pkg/assets" | ||
"github.com/tilt-dev/tilt/pkg/model" | ||
pkgsnapshot "github.com/tilt-dev/tilt/pkg/snapshot" | ||
) | ||
|
||
func Serve(ctx context.Context, rawSnapshot []byte, port int) error { | ||
buf := bytes.NewReader(rawSnapshot) | ||
var snapshot map[string]interface{} | ||
|
||
err := json.NewDecoder(buf).Decode(&snapshot) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
version, err := pkgsnapshot.GetVersionFromSnapshot(snapshot) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ss, err := newSnapshotServer(rawSnapshot, version) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
_ = ss.server.Shutdown(context.Background()) | ||
}() | ||
|
||
err = ss.serve(port) | ||
if err != nil && err != http.ErrServerClosed { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
type snapshotServer struct { | ||
assetServer assets.Server | ||
snapshot []byte | ||
server http.Server | ||
} | ||
|
||
func newSnapshotServer(snapshot []byte, version string) (*snapshotServer, error) { | ||
result := &snapshotServer{} | ||
|
||
s, err := assets.NewProdServer(assets.ProdAssetBucket, model.WebVersion(version)) | ||
if err != nil { | ||
return result, err | ||
} | ||
result.assetServer = s | ||
result.snapshot = snapshot | ||
|
||
return result, nil | ||
} | ||
|
||
func (ss *snapshotServer) serve(port int) error { | ||
m := http.NewServeMux() | ||
|
||
m.HandleFunc("/api/snapshot/local", ss.snapshotJSONHandler(ss.snapshot)) | ||
m.HandleFunc("/", ss.assetServer.ServeHTTP) | ||
|
||
ss.server = http.Server{Addr: fmt.Sprintf("0:%d", port), Handler: m} | ||
return ss.server.ListenAndServe() | ||
} | ||
|
||
func (ss *snapshotServer) snapshotJSONHandler(snapshot []byte) http.HandlerFunc { | ||
return func(w http.ResponseWriter, req *http.Request) { | ||
_, err := w.Write(snapshot) | ||
if err != nil { | ||
http.Error(w, fmt.Sprintf("error writing snapshot to http response: %v", err.Error()), http.StatusInternalServerError) | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.