merge Route into Env, rebase dagger compute
to use the new API
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
committed by
Solomon Hykes
parent
43b3af6fff
commit
cba524eb0f
@@ -1,20 +1,22 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"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",
|
||||
@@ -30,25 +32,100 @@ 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")
|
||||
}
|
||||
if err := updater.SourceFlag().Set(args[0]); err != nil {
|
||||
lg.Fatal().Err(err).Msg("invalid local source")
|
||||
name := getRouteName(lg, cmd)
|
||||
st := &dagger.RouteState{
|
||||
ID: uuid.New().String(),
|
||||
Name: name,
|
||||
LayoutSource: dagger.DirInput(args[0], []string{"*.cue", "cue.mod"}),
|
||||
}
|
||||
|
||||
if err := env.SetUpdater(updater.Value()); err != nil {
|
||||
lg.Fatal().Err(err).Msg("invalid updater script")
|
||||
for _, input := range viper.GetStringSlice("input-string") {
|
||||
parts := strings.SplitN(input, "=", 2)
|
||||
k, v := parts[0], parts[1]
|
||||
err := st.AddInput(ctx, k, dagger.TextInput(v))
|
||||
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-dir") {
|
||||
parts := strings.SplitN(input, "=", 2)
|
||||
k, v := parts[0], parts[1]
|
||||
err := st.AddInput(ctx, k, dagger.DirInput(v, []string{}))
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Str("input", k).Msg("failed to add input")
|
||||
}
|
||||
}
|
||||
|
||||
for _, input := range viper.GetStringSlice("input-git") {
|
||||
parts := strings.SplitN(input, "=", 2)
|
||||
k, v := parts[0], parts[1]
|
||||
err := st.AddInput(ctx, k, dagger.GitInput(v, "", ""))
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Str("input", k).Msg("failed to add input")
|
||||
}
|
||||
}
|
||||
|
||||
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(ctx, "", dagger.JSONInput(string(content)))
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("failed to add input")
|
||||
}
|
||||
}
|
||||
|
||||
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(ctx, "", 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("unable to initialize route")
|
||||
}
|
||||
|
||||
c, err := dagger.NewClient(ctx, "")
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("unable to create client")
|
||||
}
|
||||
output, err := c.Compute(ctx, env)
|
||||
output, err := c.Up(ctx, route)
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("failed to compute")
|
||||
}
|
||||
@@ -57,24 +134,11 @@ var computeCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
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)
|
||||
|
@@ -41,7 +41,8 @@ var newCmd = &cobra.Command{
|
||||
|
||||
if upRoute {
|
||||
lg.Info().Str("route-id", route.ID()).Msg("bringing route online")
|
||||
if err := route.Up(ctx, nil); err != nil {
|
||||
// FIXME
|
||||
if err := route.FIXME(ctx); err != nil {
|
||||
lg.Fatal().Err(err).Str("route-id", route.ID()).Msg("failed to create route")
|
||||
}
|
||||
}
|
||||
|
@@ -32,7 +32,8 @@ var upCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
// TODO: Implement options: --no-cache
|
||||
if err := route.Up(ctx, nil); err != nil {
|
||||
// FIXME
|
||||
if err := route.FIXME(ctx); err != nil {
|
||||
lg.Fatal().Err(err).Str("route-name", routeName).Str("route-id", route.ID()).Msg("failed to up the route")
|
||||
}
|
||||
},
|
||||
|
Reference in New Issue
Block a user