-
Notifications
You must be signed in to change notification settings - Fork 16
/
init.go
103 lines (97 loc) · 4.61 KB
/
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
package main
import (
log "github.com/Sirupsen/logrus"
"io/ioutil"
"github.com/codegangsta/cli"
"fmt"
"path/filepath"
Config "github.com/matthieudelaro/nut/config"
Utils "github.com/matthieudelaro/nut/utils"
Persist "github.com/matthieudelaro/nut/persist"
containerFilepath "github.com/matthieudelaro/nut/container/filepath"
)
// create a nut.yml at the current path
func initSubcommand(c *cli.Context, context Utils.Context, gitHubFlag string) {
log.Debug("initSubcommand context: ", context)
name := filepath.Base(context.GetUserDirectory())
var project Config.Project
if gitHubFlag == "" {
defaultProject := Config.NewProjectV6(nil)
defaultProject.ProjectName = name
defaultProject.DockerImage = "dockercore/golang-cross:1.13.15"
defaultProject.Macros["build"] = &Config.MacroV6{
Usage: "build the project in the container",
Actions: []string{"go build -o nut"},
ConfigV6: *Config.NewConfigV6(nil),
}
defaultProject.Macros["run"] = &Config.MacroV6{
Usage: "run the project in the container",
Actions: []string{"./nut"},
ConfigV6: *Config.NewConfigV6(nil),
}
project = defaultProject
} else {
if store, err := Persist.InitStore(context.GetUserDirectory()); err == nil {
log.Debug("Store initialized: ", store)
if filename, err := Config.DownloadFromGithub(gitHubFlag, store); err == nil {
log.Debug("Downloaded from Github to ", filename)
if parentProject, err := Config.LoadProjectFromFile(filename); err == nil {
log.Debug("Parsed from Github: ", parentProject)
if err = Config.ResolveDependencies(parentProject, store, filename); err == nil {
log.Debug("Resolved dependencies from Github: ", filename)
finalProject := Config.NewProjectV6(parentProject)
finalProject.ProjectName = name
finalProject.Base.GitHub = gitHubFlag
// modified project depending on parent configuration
// 1 - mount folder "." if not already mounted by parent configuration
mountingPointName := "main"
hostDir := "."
containerDir := containerFilepath.Join("/nut", name)
mountingPoint := &Config.VolumeV6{
Host: hostDir,
Container: containerDir,
}
if Config.CheckConflict(context, mountingPointName, mountingPoint, Config.GetVolumes(finalProject, context)) == nil {
finalProject.ConfigV6.Mount[mountingPointName] = []string{hostDir, containerDir}
}
// 2 - set working directory to "." if not specified otherwise
if Config.GetWorkingDir(finalProject) == "" {
finalProject.ConfigV6.WorkingDir = containerDir
}
project = finalProject
}
} else {
log.Error("Error while parsing from GitHub: ", err)
}
} else {
log.Error("Error while loading from GitHub: ", err)
}
} else {
log.Error("Error while loading nut files: ", err)
}
}
if project == nil {
return
}
if data, err := Config.ToYAML(project); err != nil {
log.Error("Could not generate default project configuration:", err)
} else {
fmt.Println("Project configuration:")
fmt.Println("")
dataString := string(data)
fmt.Println(dataString)
// check is nut.yml exists at the current path
nutfileName := filepath.Join(context.GetUserDirectory(), Config.NutFileName) // TODO: pick this name from a well documented and centralized list of legit nut file names
if exists, err := Utils.FileExists(nutfileName); exists && err == nil {
log.Error("Could not save new Nut project because a nut.yml file already exists")
return
} else {
err := ioutil.WriteFile(nutfileName, data, 0644) // TODO: discuss this permission level
if err != nil {
log.Error(err)
} else {
fmt.Println("Project configuration saved in ", nutfileName) // TODO: make sure that bug Project configuration saved in %!(EXTRA string=./nut.yml) is fixed.
}
}
}
}