-
Notifications
You must be signed in to change notification settings - Fork 1
/
yaml.go
78 lines (72 loc) · 1.59 KB
/
yaml.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
package main
import (
"os"
"path/filepath"
"strings"
"github.com/AlecAivazis/survey/v2"
"gopkg.in/yaml.v2"
)
// Spec is the specification of the project.
type Spec struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
EntryFile string `yaml:"entry_file"`
Dependencies []PipPackage `yaml:"dependencies"`
}
// readSpecFromPath reads spec from path.
func readSpecFromPath(path string) (Spec, error) {
spec := Spec{}
path = filepath.Join(path, "spec.yaml")
if _, err := os.Stat(path); os.IsNotExist(err) {
return spec, err
}
file, err := os.Open(path)
if err != nil {
return spec, err
}
defer file.Close()
err = yaml.NewDecoder(file).Decode(&spec)
return spec, err
}
// writeSpecToPath writes spec to path.
func writeSpecToPath(path string, spec Spec) error {
path = filepath.Join(path, "spec.yaml")
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
return yaml.NewEncoder(file).Encode(spec)
}
// makeSpecFromSurvey makes spec from survey.
func makeSpecFromSurvey() Spec {
spec := Spec{}
version, _ := getPythonVersion()
ques := []*survey.Question{
{
Name: "Name",
Prompt: &survey.Input{
Message: "project name: ",
},
Validate: survey.Required,
},
{
Name: "Version",
Prompt: &survey.Input{
Message: "python version: ",
Default: strings.TrimSpace(version),
},
Validate: survey.Required,
},
{
Name: "EntryFile",
Prompt: &survey.Input{
Message: "entry file: ",
Default: "app.py",
},
Validate: survey.Required,
},
}
survey.Ask(ques, &spec)
return spec
}