/
cmd_init.go
118 lines (97 loc) · 3.03 KB
/
cmd_init.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package cli
import (
"fmt"
"log"
"os"
"strings"
"github.com/benchkram/bob/bob/bobfile/project"
"github.com/benchkram/bob/bob/global"
"github.com/benchkram/bob/pkg/boblog"
"github.com/spf13/cobra"
)
var initCmd = &cobra.Command{
Use: "init",
Short: "Init a bob project",
Args: cobra.MaximumNArgs(1),
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
var project string
if len(args) > 0 {
project = strings.TrimSpace(args[0])
}
runInit(project)
},
}
var withoutProject = `# Pin nixpkgs to get reproducable behaviour.
nixpkgs: https://github.com/NixOS/nixpkgs/archive/refs/tags/22.05.tar.gz
# Declare tasks below the build keyword.
build:
# build - is the default task execute when calling "bob build".
build:
# Inputs of the task.
# If any of the inputs changes the task is rebuild.
# input: .
# Command to execute
cmd: echo "Can we fix it? Yes we can!"
# Rebuild policy, defaults to [on-change]
rebuild: always
# Output of a task. Can be a file, directory or docker image.
# Path must reside inside the scope of a repository.
# Is packed into an .tar.gz and stored as artifact
# in the local and/or remote cache.
# target: run
# Create pipelines by depending on other tasks.
# Bob takes care of executing tasks in the correct order
# and if possible in parallel.
# dependsOn:
`
var withProject = `project: %s
# Pin nixpkgs to get reproducable behaviour.
nixpkgs: https://github.com/NixOS/nixpkgs/archive/refs/tags/22.05.tar.gz
# Declare tasks below the build keyword.
build:
# build - is the default task execute when calling "bob build".
build:
# Inputs of the task.
# If any of the inputs changes the task is rebuild.
# input: .
# Command to execute
cmd: echo "Can we fix it? Yes we can!"
# Rebuild policy, defaults to [on-change]
rebuild: always
# Output of a task. Can be a file, directory or docker image.
# Path must reside inside the scope of a repository.
# Is packed into an .tar.gz and stored as artifact
# in the local and/or remote cache.
# target: run
# Create pipelines by depending on other tasks.
# Bob takes care of executing tasks in the correct order
# and if possible in parallel.
# dependsOn:
`
func runInit(projectName string) {
if _, err := os.Stat(global.BobFileName); err == nil {
boblog.Log.UserError(fmt.Errorf("there is already a %s in your project", global.BobFileName))
os.Exit(1)
}
wd, _ := os.Getwd()
var err error
if projectName != "" {
_, err = project.Parse(projectName)
if err != nil {
boblog.Log.UserError(err)
os.Exit(1)
}
err = createBobfile(fmt.Sprintf(withProject, projectName))
fmt.Printf("Initialized basic %s in %s\n", global.BobFileName, wd)
} else {
err = createBobfile(withoutProject)
fmt.Printf("Initialized basic %s in %s\n", global.BobFileName, wd)
}
if err != nil {
log.Fatal(err)
}
}
func createBobfile(content string) error {
return os.WriteFile(global.BobFileName, []byte(content), 0664)
}