Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
tui: dedicated outputs for mosaic layout
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Jul 7, 2024
1 parent 96aac24 commit 2c9155e
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 50 deletions.
13 changes: 7 additions & 6 deletions cmd/sst/mosaic/mosaic.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func CmdMosaic(c *cli.Cli) error {
if err != nil {
return err
}
for name, args := range p.App().Providers {
for name, a := range p.App().Providers {
args := a
switch name {
case "aws":
wg.Go(func() error {
Expand Down Expand Up @@ -127,13 +128,13 @@ func CmdMosaic(c *cli.Cli) error {
case unknown := <-evts:
switch evt := unknown.(type) {
case *project.CompleteEvent:
for _, r := range evt.Receivers {
if r.Dev.Command == "" {
for _, d := range evt.Devs {
if d.Command == "" {
continue
}
dir := filepath.Join(cwd, r.Directory)
slog.Info("mosaic", "receiver", r.Name, "directory", dir)
multi.AddPane(r.Name, append([]string{currentExecutable, "mosaic"}, strings.Split(r.Dev.Command, " ")...), r.Name, dir, true)
dir := filepath.Join(cwd, d.Directory)
slog.Info("mosaic", "dev", d.Name, "directory", dir)
multi.AddPane(d.Name, append([]string{currentExecutable, "mosaic"}, strings.Split(d.Command, " ")...), d.Name, dir, true)
}
break
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/sst/mosaic/multiplexer/multiplexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,19 @@ func (m *Model) draw() {
style := tcell.StyleDefault
if index == m.selected {
style = style.Background(tcell.ColorGray)
style = style.Bold(true)
if m.focus == "sidebar" {
style = style.Background(tcell.ColorOrangeRed)
}
style = style.Foreground(tcell.ColorWhite)
title.SetRight("< ", style)
}

text := item.title
title.SetStyle(style)
title.SetLeft(" "+text, tcell.StyleDefault)
if item.status == paneStatusStopped {
title.SetRight("(-)", tcell.StyleDefault)
title.SetRight("- ", tcell.StyleDefault)
}
m.sidebarWidget.AddWidget(title, 0)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/sst/mosaic/server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func Stream(ctx context.Context, url string, types ...interface{}) (chan any, er
return out, nil
}

func Env(ctx context.Context, receiverID string, url string) (map[string]string, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url+"/api/env?receiverID="+receiverID, nil)
func Env(ctx context.Context, directory string, url string) (map[string]string, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url+"/api/env?directory="+directory, nil)
if err != nil {
return nil, err
}
Expand Down
32 changes: 16 additions & 16 deletions cmd/sst/mosaic/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,31 @@ func (s *Server) Start(ctx context.Context, p *project.Project) error {
})

s.Mux.HandleFunc("/api/env", func(w http.ResponseWriter, r *http.Request) {
receiverID := r.URL.Query().Get("receiverID")
var receiver *project.Receiver
directory := r.URL.Query().Get("directory")
var dev *project.Dev
cwd, _ := os.Getwd()
for _, r := range complete.Receivers {
full := filepath.Join(cwd, r.Directory)
slog.Info("matching receiver", "full", full, "receiverID", receiverID)
if full == receiverID {
receiver = &r
for _, d := range complete.Devs {
full := filepath.Join(cwd, d.Directory)
slog.Info("matching dev", "full", full, "directory", directory)
if full == directory {
dev = &d
break
}
}
if receiver == nil {
slog.Info("receiver not found", "receiverID", receiverID)
http.Error(w, "receiver not found", http.StatusNotFound)
if dev == nil {
slog.Info("dev not found", "directory", directory)
http.Error(w, "dev not found", http.StatusNotFound)
return
}
env := map[string]string{}
if receiver.Aws != nil && receiver.Aws.Role != "" {
if dev.Aws != nil && dev.Aws.Role != "" {
prov, _ := p.Provider("aws")
awsProvider := prov.(*provider.AwsProvider)
stsClient := sts.NewFromConfig(awsProvider.Config())
sessionName := "sst-dev"
slog.Info("assuming role", "role", receiver.Aws.Role)
slog.Info("assuming role", "role", dev.Aws.Role)
result, err := stsClient.AssumeRole(r.Context(), &sts.AssumeRoleInput{
RoleArn: &receiver.Aws.Role,
RoleArn: &dev.Aws.Role,
RoleSessionName: &sessionName,
DurationSeconds: awssdk.Int32(3600),
})
Expand All @@ -128,14 +128,14 @@ func (s *Server) Start(ctx context.Context, p *project.Project) error {
env["AWS_SECRET_ACCESS_KEY"] = *result.Credentials.SecretAccessKey
env["AWS_SESSION_TOKEN"] = *result.Credentials.SessionToken
}
slog.Info("receiver", "links", receiver.Links)
for _, resource := range receiver.Links {
slog.Info("dev", "links", dev.Links)
for _, resource := range dev.Links {
value := complete.Links[resource]
jsonValue, _ := json.Marshal(value)
env["SST_RESOURCE_"+resource] = string(jsonValue)
}
env["SST_RESOURCE_App"] = fmt.Sprintf(`{"name": "%s", "stage": "%s" }`, p.App().Name, p.App().Stage)
for key, value := range receiver.Environment {
for key, value := range dev.Environment {
slog.Info("setting env", "key", key, "value", value)
env[key] = value
}
Expand Down
27 changes: 13 additions & 14 deletions pkg/platform/src/components/aws/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,23 @@ export class Astro extends Component implements Link.Linkable {
const { sitePath, partition } = prepare(args, opts);

if ($dev) {
const server = createDevServer(parent, name, args);
this.registerOutputs({
_metadata: {
mode: "placeholder",
path: sitePath,
server: createDevServer(parent, name, args).arn,
server: server.arn,
},
_dev: {
directory: sitePath,
links: output(args.link || [])
.apply(Link.build)
.apply((links) => links.map((link) => link.name)),
aws: {
role: server.nodes.role.arn,
},
environment: args.environment,
command: "npm run dev",
},
});
return;
Expand Down Expand Up @@ -372,19 +384,6 @@ export class Astro extends Component implements Link.Linkable {
url: distribution.apply((d) => d.domainUrl ?? d.url),
edge: plan.edge,
server: serverFunction.arn,
dev: {
command: "npm run dev",
},
},
_receiver: {
directory: sitePath,
links: [],
environment: output(args.environment).apply((env) => ({
...env,
})),
dev: {
command: "npm run dev",
},
},
});

Expand Down
6 changes: 6 additions & 0 deletions pkg/platform/src/components/aws/nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ export class Nextjs extends Component implements Link.Linkable {
path: sitePath,
server: createDevServer(parent, name, args).arn,
},
_dev: {
directory: sitePath,
dev: {
command: "npm run dev",
},
},
});
return;
}
Expand Down
16 changes: 14 additions & 2 deletions pkg/platform/src/components/aws/nuxt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import path from "path";
import { ComponentResourceOptions, Output, all } from "@pulumi/pulumi";
import { ComponentResourceOptions, Output, all, output } from "@pulumi/pulumi";
import { Function } from "./function.js";
import {
SsrSiteArgs,
Expand Down Expand Up @@ -323,11 +323,23 @@ export class Nuxt extends Component implements Link.Linkable {
const parent = this;
const { sitePath, partition } = prepare(args, opts);
if ($dev) {
const server = createDevServer(parent, name, args);
this.registerOutputs({
_metadata: {
mode: "placeholder",
path: sitePath,
server: createDevServer(parent, name, args).arn,
server: server.arn,
},
_dev: {
directory: sitePath,
links: output(args.link || [])
.apply(Link.build)
.apply((links) => links.map((link) => link.name)),
aws: {
role: server.nodes.role.arn,
},
environment: args.environment,
command: "npm run dev",
},
});
return;
Expand Down
14 changes: 13 additions & 1 deletion pkg/platform/src/components/aws/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,24 @@ export class React extends Component implements Link.Linkable {
const edge = normalizeEdge();
const { sitePath, partition } = prepare(args, opts);
if ($dev) {
const server = createDevServer(parent, name, args);
this.registerOutputs({
_metadata: {
mode: "placeholder",
path: sitePath,
edge,
server: createDevServer(parent, name, args).arn,
server: server.arn,
},
_dev: {
directory: sitePath,
links: output(args.link || [])
.apply(Link.build)
.apply((links) => links.map((link) => link.name)),
aws: {
role: server.nodes.role.arn,
},
environment: args.environment,
command: "npm run dev",
},
});
return;
Expand Down
14 changes: 13 additions & 1 deletion pkg/platform/src/components/aws/remix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,24 @@ export class Remix extends Component implements Link.Linkable {
const edge = normalizeEdge();
const { sitePath, partition } = prepare(args, opts);
if ($dev) {
const server = createDevServer(parent, name, args);
this.registerOutputs({
_metadata: {
mode: "placeholder",
path: sitePath,
edge,
server: createDevServer(parent, name, args).arn,
server: server.arn,
},
_dev: {
directory: sitePath,
links: output(args.link || [])
.apply(Link.build)
.apply((links) => links.map((link) => link.name)),
aws: {
role: server.nodes.role.arn,
},
environment: args.environment,
command: "npm run dev",
},
});
return;
Expand Down
17 changes: 16 additions & 1 deletion pkg/platform/src/components/aws/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,22 @@ export class Service extends Component implements Link.Linkable {
? path.dirname(imageArgs.dockerfile)
: imageArgs.context,
),
dev: args.dev,
links: linkData.apply((input) => input.map((item) => item.name)),
environment: {
...args.environment,
AWS_REGION: region,
},
aws: {
role: taskRole.arn,
},
})),
_dev: imageArgs.apply((imageArgs) => ({
directory: path.join(
imageArgs.dockerfile
? path.dirname(imageArgs.dockerfile)
: imageArgs.context,
),
command: output(args.dev).apply((v) => v?.command),
links: linkData.apply((input) => input.map((item) => item.name)),
environment: {
...args.environment,
Expand Down
16 changes: 14 additions & 2 deletions pkg/platform/src/components/aws/solid-start.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import path from "path";
import { ComponentResourceOptions, Output, all } from "@pulumi/pulumi";
import { ComponentResourceOptions, Output, all, output } from "@pulumi/pulumi";
import { Function } from "./function.js";
import {
SsrSiteArgs,
Expand Down Expand Up @@ -324,11 +324,23 @@ export class SolidStart extends Component implements Link.Linkable {
const parent = this;
const { sitePath, partition } = prepare(args, opts);
if ($dev) {
const server = createDevServer(parent, name, args);
this.registerOutputs({
_metadata: {
mode: "placeholder",
path: sitePath,
server: createDevServer(parent, name, args).arn,
server: server.arn,
},
_dev: {
directory: sitePath,
links: output(args.link || [])
.apply(Link.build)
.apply((links) => links.map((link) => link.name)),
aws: {
role: server.nodes.role.arn,
},
environment: args.environment,
command: "npm run dev",
},
});
return;
Expand Down
14 changes: 13 additions & 1 deletion pkg/platform/src/components/aws/svelte-kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,24 @@ export class SvelteKit extends Component implements Link.Linkable {
const { sitePath, partition } = prepare(args, opts);

if ($dev) {
const server = createDevServer(parent, name, args);
this.registerOutputs({
_metadata: {
mode: "placeholder",
path: sitePath,
edge,
server: createDevServer(parent, name, args).arn,
server: server.arn,
},
_dev: {
directory: sitePath,
links: output(args.link || [])
.apply(Link.build)
.apply((links) => links.map((link) => link.name)),
aws: {
role: server.nodes.role.arn,
},
environment: args.environment,
command: "npm run dev",
},
});
return;
Expand Down
5 changes: 5 additions & 0 deletions pkg/platform/src/components/base/base-static-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ export function cleanup(
) {
return {
_hint: url,
_dev: {
directory: sitePath,
environment: environment,
command: "npm run dev",
},
_receiver: all([sitePath, environment]).apply(
([sitePath, environment]) => ({
directory: sitePath,
Expand Down
Loading

0 comments on commit 2c9155e

Please sign in to comment.