implemented inputs

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-03-26 15:50:18 -07:00
parent ef84d2d431
commit 576613e46a
12 changed files with 91 additions and 42 deletions

View File

@ -1,4 +1,4 @@
package cmd
package common
import (
"context"
@ -11,7 +11,19 @@ import (
)
// getCurrentRoute returns the current selected route based on its abs path
func getCurrentRoute(ctx context.Context, store *dagger.Store) *dagger.Route {
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)
var (
@ -37,15 +49,10 @@ func getCurrentRoute(ctx context.Context, store *dagger.Store) *dagger.Route {
}
}
route, err := dagger.NewRoute(st)
if err != nil {
lg.Fatal().Err(err).Interface("routeState", st).Msg("failed to init route")
return st
}
return route
}
func routeUp(ctx context.Context, route *dagger.Route) {
func RouteUp(ctx context.Context, route *dagger.Route) {
lg := log.Ctx(ctx)
c, err := dagger.NewClient(ctx, "")

View File

@ -6,6 +6,7 @@ import (
"os"
"strings"
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"go.mozilla.org/sops"
@ -131,7 +132,7 @@ var computeCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("unable to initialize route")
}
routeUp(ctx, route)
common.RouteUp(ctx, route)
},
}

View File

@ -2,6 +2,7 @@
package cmd
import (
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
@ -29,7 +30,7 @@ var downCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("failed to load store")
}
route := getCurrentRoute(ctx, store)
route := common.GetCurrentRoute(ctx, store)
// TODO: Implement options: --no-cache
if err := route.Down(ctx, nil); err != nil {

View File

@ -1,14 +1,16 @@
package input
import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var containerCmd = &cobra.Command{
Use: "container ID",
Use: "container TARGET CONTAINER-IMAGE",
Short: "Add a container image as input artifact",
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(2),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
@ -17,10 +19,10 @@ var containerCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
// lg := logger.New()
// ctx := lg.WithContext(cmd.Context())
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
panic("not implemented")
updateRouteInput(ctx, args[0], dagger.DockerInput(args[1]))
},
}

View File

@ -1,14 +1,16 @@
package input
import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var dirCmd = &cobra.Command{
Use: "dir PATH",
Use: "dir TARGET PATH",
Short: "Add a local directory as input artifact",
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(2),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
@ -17,10 +19,10 @@ var dirCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
// lg := logger.New()
// ctx := lg.WithContext(cmd.Context())
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
panic("not implemented")
updateRouteInput(ctx, args[0], dagger.DirInput(args[1], []string{}))
},
}

View File

@ -1,14 +1,16 @@
package input
import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var gitCmd = &cobra.Command{
Use: "git REMOTE REF [SUBDIR]",
Use: "git TARGET REMOTE REF [SUBDIR]",
Short: "Add a git repository as input artifact",
Args: cobra.MinimumNArgs(2),
Args: cobra.RangeArgs(3, 4),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
@ -17,10 +19,15 @@ var gitCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
// lg := logger.New()
// ctx := lg.WithContext(cmd.Context())
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
panic("not implemented")
subDir := ""
if len(args) > 3 {
subDir = args[3]
}
updateRouteInput(ctx, args[0], dagger.GitInput(args[1], args[2], subDir))
},
}

View File

@ -1,6 +1,13 @@
package input
import "github.com/spf13/cobra"
import (
"context"
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/dagger"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
// Cmd exposes the top-level command
var Cmd = &cobra.Command{
@ -14,6 +21,23 @@ func init() {
gitCmd,
containerCmd,
secretCmd,
valueCmd,
textCmd,
)
}
func updateRouteInput(ctx context.Context, target string, input dagger.Input) {
lg := log.Ctx(ctx)
store, err := dagger.DefaultStore()
if err != nil {
lg.Fatal().Err(err).Msg("failed to load store")
}
st := common.GetCurrentRouteState(ctx, store)
st.AddInput(target, input)
if err := store.UpdateRoute(ctx, st, nil); err != nil {
lg.Fatal().Err(err).Str("routeId", st.ID).Str("routeName", st.Name).Msg("cannot update route")
}
lg.Info().Str("routeId", st.ID).Str("routeName", st.Name).Msg("updated route")
}

View File

@ -6,9 +6,9 @@ import (
)
var secretCmd = &cobra.Command{
Use: "secret VALUE",
Use: "secret TARGET VALUE",
Short: "Add an encrypted input secret",
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(2),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233

View File

@ -1,14 +1,16 @@
package input
import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var valueCmd = &cobra.Command{
Use: "value VALUE",
Short: "Add an input value",
Args: cobra.ExactArgs(1),
var textCmd = &cobra.Command{
Use: "text TARGET VALUE",
Short: "Add an input text",
Args: cobra.ExactArgs(2),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
@ -17,15 +19,15 @@ var valueCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
// lg := logger.New()
// ctx := lg.WithContext(cmd.Context())
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
panic("not implemented")
updateRouteInput(ctx, args[0], dagger.TextInput(args[1]))
},
}
func init() {
if err := viper.BindPFlags(valueCmd.Flags()); err != nil {
if err := viper.BindPFlags(textCmd.Flags()); err != nil {
panic(err)
}
}

View File

@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
@ -56,7 +57,7 @@ var newCmd = &cobra.Command{
}
if viper.GetBool("up") {
routeUp(ctx, route)
common.RouteUp(ctx, route)
}
},
}

View File

@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
@ -30,7 +31,7 @@ var queryCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("failed to load store")
}
route := getCurrentRoute(ctx, store)
route := common.GetCurrentRoute(ctx, store)
expr := args[0]

View File

@ -2,6 +2,7 @@
package cmd
import (
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
@ -28,10 +29,10 @@ var upCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("failed to load store")
}
route := getCurrentRoute(ctx, store)
route := common.GetCurrentRoute(ctx, store)
// TODO: Implement options: --no-cache
routeUp(ctx, route)
common.RouteUp(ctx, route)
},
}