|
| 1 | +// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved. |
| 2 | +// Use of this source code is governed by the license in the LICENSE file. |
| 3 | + |
| 4 | +package boxcli |
| 5 | + |
| 6 | +import ( |
| 7 | + "github.com/pkg/errors" |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "go.jetpack.io/devbox" |
| 10 | + "go.jetpack.io/devbox/internal/impl/devopt" |
| 11 | + "go.jetpack.io/devbox/internal/integrations/envsec" |
| 12 | +) |
| 13 | + |
| 14 | +type envsecInitCmdFlags struct { |
| 15 | + config configFlags |
| 16 | +} |
| 17 | + |
| 18 | +func envsecCmd() *cobra.Command { |
| 19 | + cmd := &cobra.Command{ |
| 20 | + Use: "envsec", |
| 21 | + Short: "envsec commands", |
| 22 | + } |
| 23 | + cmd.AddCommand(envsecInitCmd()) |
| 24 | + cmd.Hidden = true |
| 25 | + return cmd |
| 26 | +} |
| 27 | + |
| 28 | +func envsecInitCmd() *cobra.Command { |
| 29 | + flags := envsecInitCmdFlags{} |
| 30 | + cmd := &cobra.Command{ |
| 31 | + Use: "init", |
| 32 | + Short: "initialize envsec integration", |
| 33 | + Args: cobra.ExactArgs(0), |
| 34 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 35 | + return envsecInitFunc(cmd, flags) |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + flags.config.register(cmd) |
| 40 | + |
| 41 | + return cmd |
| 42 | +} |
| 43 | + |
| 44 | +func envsecInitFunc(cmd *cobra.Command, flags envsecInitCmdFlags) error { |
| 45 | + box, err := devbox.Open(&devopt.Opts{ |
| 46 | + Dir: flags.config.path, |
| 47 | + Stderr: cmd.ErrOrStderr(), |
| 48 | + }) |
| 49 | + if err != nil { |
| 50 | + return errors.WithStack(err) |
| 51 | + } |
| 52 | + if err := envsec.EnsureInitialized( |
| 53 | + cmd.Context(), box.ProjectDir()); err == nil { |
| 54 | + return nil |
| 55 | + } |
| 56 | + box.Config().SetStringField("EnvFrom", "envsec") |
| 57 | + return box.Config().SaveTo(box.ProjectDir()) |
| 58 | +} |
0 commit comments