Öffentliche Dateiansicht: Raw-Dateien, Tree, Releases und Issues sind ohne Login verfügbar.
cmd/init.go Raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package cmd

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/spf13/cobra"
)

func init() {
	rootCmd.AddCommand(newInitCmd())
}

func newInitCmd() *cobra.Command {
	var force bool

	cmd := &cobra.Command{
		Use:   "init [project-name]",
		Short: "Create a .envault file in the current directory",
		Args:  cobra.MaximumNArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			var name string
			if len(args) == 1 {
				name = args[0]
			} else {
				cwd, err := os.Getwd()
				if err != nil {
					return err
				}
				name = filepath.Base(cwd)
			}
			name = strings.TrimSpace(name)
			if name == "" {
				return fmt.Errorf("project name must not be empty")
			}

			const filename = ".envault"
			if !force {
				if _, err := os.Stat(filename); err == nil {
					return fmt.Errorf("%s already exists (use --force to overwrite)", filename)
				}
			}

			content := fmt.Sprintf("project=%s\n", name)
			if err := os.WriteFile(filename, []byte(content), 0644); err != nil {
				return err
			}

			fmt.Fprintf(os.Stderr, "Created .envault for project %q\n", name)
			return nil
		},
	}

	cmd.Flags().BoolVarP(&force, "force", "f", false, "overwrite existing .envault file")
	return cmd
}