Skip to content

Commit dd92221

Browse files
committed
Add dunner init command which initializes with default dunner task file
1 parent 8becd1b commit dd92221

File tree

5 files changed

+158
-5
lines changed

5 files changed

+158
-5
lines changed

cmd/initialize.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cmd
2+
3+
import (
4+
"github.com/leopardslab/dunner/pkg/initialize"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func init() {
9+
rootCmd.AddCommand(initCmd)
10+
}
11+
12+
var initCmd = &cobra.Command{
13+
Use: "init",
14+
Short: "Generates a dunner task file `.dunner.yaml`",
15+
Long: "You can initialize any project with dunner task file. It generates a default task file `.dunner.yaml`, you can customize it based on needs. You can override the name of task file using -t flag.",
16+
Run: initialize.Initialize,
17+
Args: cobra.NoArgs,
18+
Aliases: []string{"i"},
19+
}

cmd/validate.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ func init() {
1414
}
1515

1616
var validateCmd = &cobra.Command{
17-
Use: "validate",
18-
Short: "Validate the dunner task file `.dunner.yaml`",
19-
Long: "You can validate task file `.dunner.yaml` with this command to see if there are any parse errors",
20-
Run: Validate,
21-
Args: cobra.MinimumNArgs(0),
17+
Use: "validate",
18+
Short: "Validate the dunner task file `.dunner.yaml`",
19+
Long: "You can validate task file `.dunner.yaml` with this command to see if there are any parse errors",
20+
Run: Validate,
21+
Args: cobra.NoArgs,
22+
Aliases: []string{"v"},
2223
}
2324

2425
// Validate command invoked from command line, validates the dunner task file. If there are errors, it fails with non-zero exit code.

internal/constants.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package internal
2+
3+
// DefaultTaskFileContents is the default dunner taskfile contents, used when initialized with dunner
4+
const DefaultTaskFileContents = `# This is an example dunner task file. Please make any required changes.
5+
build:
6+
- name: setup
7+
# Image name that has to be pulled from a registry
8+
image: node:latest
9+
# List of commands that has to be run inside the container
10+
commands:
11+
- ["npm", "--version"]
12+
- ["npm", "install"]
13+
# List of directories that are to be mounted on the container
14+
mounts:
15+
- /tmp:/tmp:w
16+
# Set any environment variables to be exported in the container
17+
envs:
18+
- PERM=775
19+
`
20+
21+
// DefaultTaskFilePermission is the default file permission of dunner task file
22+
const DefaultTaskFilePermission = 0644

pkg/initialize/initialize.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package initialize
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
8+
"github.com/leopardslab/dunner/internal"
9+
"github.com/leopardslab/dunner/internal/logger"
10+
"github.com/spf13/cobra"
11+
"github.com/spf13/viper"
12+
)
13+
14+
// Initialize command invoked from command line generates a dunner task file with default template
15+
func Initialize(_ *cobra.Command, args []string) {
16+
var dunnerFile = viper.GetString("DunnerTaskFile")
17+
if err := initProject(dunnerFile); err != nil {
18+
logger.Log.Fatalf("Failed to initialize project: %s", err.Error())
19+
}
20+
logger.Log.Infof("Dunner task file `%s` created. Please make any required changes.", dunnerFile)
21+
}
22+
23+
func initProject(filename string) error {
24+
if _, err := os.Stat(filename); !os.IsNotExist(err) {
25+
if err != nil {
26+
return err
27+
}
28+
return fmt.Errorf("%s already exists", filename)
29+
}
30+
logger.Log.Infof("Generating %s file", filename)
31+
return ioutil.WriteFile(filename, []byte(internal.DefaultTaskFileContents), internal.DefaultTaskFilePermission)
32+
}

pkg/initialize/initialize_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package initialize
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"testing"
8+
9+
"github.com/leopardslab/dunner/internal"
10+
"github.com/leopardslab/dunner/pkg/config"
11+
yaml "gopkg.in/yaml.v2"
12+
)
13+
14+
func setup(t *testing.T) func() {
15+
folder, err := ioutil.TempDir("", "")
16+
if err != nil {
17+
t.Errorf("Failed to create temp dir: %s", err.Error())
18+
}
19+
20+
previous, err := os.Getwd()
21+
if err != nil {
22+
t.Errorf("Failed to get working directory: %s", err.Error())
23+
}
24+
25+
if err = os.Chdir(folder); err != nil {
26+
t.Errorf("Failed to change working directory: %s", err.Error())
27+
}
28+
return func() {
29+
if err = os.Chdir(previous); err != nil {
30+
t.Errorf("Failed to revert change in working directory: %s", err.Error())
31+
}
32+
}
33+
}
34+
35+
func TestInitializeSuccess(t *testing.T) {
36+
revert := setup(t)
37+
defer revert()
38+
var filename = ".test_dunner.yml"
39+
if err := initProject(filename); err != nil {
40+
t.Errorf("Failed to open dunner task file %s: %s", filename, err.Error())
41+
}
42+
43+
file, err := os.Open(filename)
44+
if err != nil {
45+
t.Errorf("Failed to open dunner task file %s: %s", filename, err.Error())
46+
}
47+
48+
fileContents, err := ioutil.ReadAll(file)
49+
if err != nil {
50+
t.Errorf("Failed to read dunner task file %s: %s", filename, err.Error())
51+
}
52+
53+
var configs config.Configs
54+
if err := yaml.Unmarshal(fileContents, &configs.Tasks); err != nil {
55+
t.Errorf("Task file config structure invalid: %s", err.Error())
56+
}
57+
}
58+
59+
func TestInitializeWhenFileExists(t *testing.T) {
60+
revert := setup(t)
61+
defer revert()
62+
var filename = ".test_dunner.yml"
63+
createFile(t, filename, internal.DefaultTaskFileContents)
64+
65+
expected := fmt.Sprintf("%s already exists", filename)
66+
err := initProject(filename)
67+
if err == nil {
68+
t.Errorf("expected: %s, got nil", expected)
69+
}
70+
if expected != err.Error() {
71+
t.Errorf("expected: %s, got: %s", expected, err.Error())
72+
}
73+
}
74+
75+
func createFile(t *testing.T, filename, contents string) {
76+
if err := ioutil.WriteFile(filename, []byte(contents), 0644); err != nil {
77+
t.Errorf("Failed to create file: %s", err.Error())
78+
}
79+
}

0 commit comments

Comments
 (0)