Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions cmd/dagger/cmd/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package common

import (
"context"
"fmt"
"os"

"dagger.io/go/dagger"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

// getCurrentRoute returns the current selected route based on its abs path
func GetCurrentRoute(ctx context.Context, store *dagger.Store) *dagger.Route {
lg := log.Ctx(ctx)
st := GetCurrentRouteState(ctx, store)

route, err := dagger.NewRoute(st)
if err != nil {
lg.
Fatal().
Err(err).
Interface("routeState", st).
Msg("failed to init route")
}

return route
}

func GetCurrentRouteState(ctx context.Context, store *dagger.Store) *dagger.RouteState {
lg := log.Ctx(ctx)

routeName := viper.GetString("route")
if routeName != "" {
st, err := store.LookupRouteByName(ctx, routeName)
if err != nil {
lg.
Fatal().
Err(err).
Str("routeName", routeName).
Msg("failed to lookup route by name")
}
return st
}

wd, err := os.Getwd()
if err != nil {
lg.Fatal().Err(err).Msg("cannot get current working directory")
}
st, err := store.LookupRouteByPath(ctx, wd)
if err != nil {
lg.
Fatal().
Err(err).
Str("routePath", wd).
Msg("failed to lookup route by path")
}
return st
}

func RouteUp(ctx context.Context, route *dagger.Route) {
lg := log.Ctx(ctx)

c, err := dagger.NewClient(ctx, "")
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}
output, err := c.Up(ctx, route)
if err != nil {
lg.Fatal().Err(err).Msg("failed to compute")
}
fmt.Println(output.JSON())
}
145 changes: 106 additions & 39 deletions cmd/dagger/cmd/compute.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package cmd

import (
"fmt"
"encoding/json"
"errors"
"os"
"strings"

"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"go.mozilla.org/sops"
"go.mozilla.org/sops/decrypt"

"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
input *dagger.InputValue
updater *dagger.InputValue
)

var computeCmd = &cobra.Command{
Use: "compute CONFIG",
Short: "Compute a configuration",
Expand All @@ -30,51 +32,116 @@ var computeCmd = &cobra.Command{
lg := logger.New()
ctx := lg.WithContext(cmd.Context())

env, err := dagger.NewEnv()
if err != nil {
lg.Fatal().Err(err).Msg("unable to initialize environment")
st := &dagger.RouteState{
ID: uuid.New().String(),
Name: "FIXME",
LayoutSource: dagger.DirInput(args[0], []string{"*.cue", "cue.mod"}),
}
if err := updater.SourceFlag().Set(args[0]); err != nil {
lg.Fatal().Err(err).Msg("invalid local source")

for _, input := range viper.GetStringSlice("input-string") {
parts := strings.SplitN(input, "=", 2)
k, v := parts[0], parts[1]
err := st.AddInput(k, dagger.TextInput(v))
if err != nil {
lg.
Fatal().
Err(err).
Str("input", k).
Msg("failed to add input")
}
}

if err := env.SetUpdater(updater.Value()); err != nil {
lg.Fatal().Err(err).Msg("invalid updater script")
for _, input := range viper.GetStringSlice("input-dir") {
parts := strings.SplitN(input, "=", 2)
k, v := parts[0], parts[1]
err := st.AddInput(k, dagger.DirInput(v, []string{}))
if err != nil {
lg.
Fatal().
Err(err).
Str("input", k).
Msg("failed to add input")
}
}
if err := env.SetInput(input.Value()); err != nil {
lg.Fatal().Err(err).Msg("invalid input")

for _, input := range viper.GetStringSlice("input-git") {
parts := strings.SplitN(input, "=", 2)
k, v := parts[0], parts[1]
err := st.AddInput(k, dagger.GitInput(v, "", ""))
if err != nil {
lg.
Fatal().
Err(err).
Str("input", k).
Msg("failed to add input")
}
}
c, err := dagger.NewClient(ctx, "")
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")

if f := viper.GetString("input-json"); f != "" {
lg := lg.With().Str("path", f).Logger()

content, err := os.ReadFile(f)
if err != nil {
lg.Fatal().Err(err).Msg("failed to read file")
}

plaintext, err := decrypt.Data(content, "json")
if err != nil && !errors.Is(err, sops.MetadataNotFound) {
lg.Fatal().Err(err).Msg("unable to decrypt")
}

if len(plaintext) > 0 {
content = plaintext
}

if !json.Valid(content) {
lg.Fatal().Msg("invalid json")
}

err = st.AddInput("", dagger.JSONInput(string(content)))
if err != nil {
lg.Fatal().Err(err).Msg("failed to add input")
}
}
output, err := c.Compute(ctx, env)

if f := viper.GetString("input-yaml"); f != "" {
lg := lg.With().Str("path", f).Logger()

content, err := os.ReadFile(f)
if err != nil {
lg.Fatal().Err(err).Msg("failed to read file")
}

plaintext, err := decrypt.Data(content, "yaml")
if err != nil && !errors.Is(err, sops.MetadataNotFound) {
lg.Fatal().Err(err).Msg("unable to decrypt")
}

if len(plaintext) > 0 {
content = plaintext
}

err = st.AddInput("", dagger.YAMLInput(string(content)))
if err != nil {
lg.Fatal().Err(err).Msg("failed to add input")
}
}

route, err := dagger.NewRoute(st)
if err != nil {
lg.Fatal().Err(err).Msg("failed to compute")
lg.Fatal().Err(err).Msg("unable to initialize route")
}
fmt.Println(output.JSON())

common.RouteUp(ctx, route)
},
}

func init() {
var err error
// Setup --input-* flags
input, err = dagger.NewInputValue("{}")
if err != nil {
panic(err)
}
computeCmd.Flags().Var(input.StringFlag(), "input-string", "TARGET=STRING")
computeCmd.Flags().Var(input.DirFlag(), "input-dir", "TARGET=PATH")
computeCmd.Flags().Var(input.GitFlag(), "input-git", "TARGET=REMOTE#REF")
computeCmd.Flags().Var(input.CueFlag(), "input-cue", "CUE")
computeCmd.Flags().Var(input.JSONFlag(), "input-json", "JSON")
computeCmd.Flags().Var(input.YAMLFlag(), "input-yaml", "YAML")

// Setup (future) --from-* flags
updater, err = dagger.NewInputValue("[...{do:string, ...}]")
if err != nil {
panic(err)
}
computeCmd.Flags().StringSlice("input-string", []string{}, "TARGET=STRING")
computeCmd.Flags().StringSlice("input-dir", []string{}, "TARGET=PATH")
computeCmd.Flags().StringSlice("input-git", []string{}, "TARGET=REMOTE#REF")
computeCmd.Flags().String("input-json", "", "JSON")
computeCmd.Flags().String("input-yaml", "", "YAML")

if err := viper.BindPFlags(computeCmd.Flags()); err != nil {
panic(err)
Expand Down
31 changes: 31 additions & 0 deletions cmd/dagger/cmd/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a route after taking it offline (WARNING: may destroy infrastructure)",
Args: cobra.NoArgs,
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
// lg := logger.New()
// ctx := lg.WithContext(cmd.Context())

panic("not implemented")
},
}

func init() {
if err := viper.BindPFlags(deleteCmd.Flags()); err != nil {
panic(err)
}
}
52 changes: 52 additions & 0 deletions cmd/dagger/cmd/down.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var downCmd = &cobra.Command{
Use: "down",
Short: "Take a route offline (WARNING: may destroy infrastructure)",
Args: cobra.NoArgs,
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())

store, err := dagger.DefaultStore()
if err != nil {
lg.Fatal().Err(err).Msg("failed to load store")
}

route := common.GetCurrentRoute(ctx, store)

// TODO: Implement options: --no-cache
if err := route.Down(ctx, nil); err != nil {
lg.
Fatal().
Err(err).
Str("routeName", route.Name()).
Str("routeId", route.ID()).
Msg("failed to up the route")
}
},
}

func init() {
downCmd.Flags().Bool("--no-cache", false, "Disable all run cache")

if err := viper.BindPFlags(downCmd.Flags()); err != nil {
panic(err)
}
}
31 changes: 31 additions & 0 deletions cmd/dagger/cmd/history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var historyCmd = &cobra.Command{
Use: "history",
Short: "List past changes to a route",
Args: cobra.NoArgs,
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
// lg := logger.New()
// ctx := lg.WithContext(cmd.Context())

panic("not implemented")
},
}

func init() {
if err := viper.BindPFlags(historyCmd.Flags()); err != nil {
panic(err)
}
}
Loading