-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (115 loc) · 3.71 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"github.com/AlecAivazis/survey/v2"
"github.com/alexfacciorusso/winget-generate/debug"
"github.com/alexfacciorusso/winget-generate/style"
"github.com/alexfacciorusso/winget-generate/suggestion"
"github.com/fatih/color"
"gopkg.in/yaml.v2"
)
type manifestData struct {
Name string `yaml:"Name"`
Publisher string `yaml:"Publisher"`
ID string `yaml:"Id"`
License string `yaml:"License"`
LicenseURL string `yaml:"LicenseUrl"`
Version string `yaml:"Version"`
}
func main() {
verbose := flag.Bool("-v", false, "")
flag.Parse()
if *verbose {
log.SetOutput(debug.DebugWriter)
} else {
log.SetOutput(ioutil.Discard)
}
var manifest manifestData
fmt.Println("Hello! We are going to generate a manifest for winget.")
// region GitHub
var githubURL string
survey.AskOne(&survey.Input{
Message: fmt.Sprintf("If your app is on GitHub, insert its %s now, or leave empty otherwise:", style.QuestionElement("URL")),
}, &githubURL)
suggestions := suggestion.GetSuggestionsForRepo(githubURL)
// endregion
// region Name and Publisher
prompt := []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{
Message: fmt.Sprintf("What's the %s of the app? [E.g. Telegram]", style.QuestionElement("name")),
Default: suggestions.Name,
},
},
{
Name: "publisher",
Prompt: &survey.Input{
Message: fmt.Sprintf("Who is the %s of the app? E.g. Telegram Messenger Inc.", style.QuestionElement("publisher")),
Default: suggestions.Publisher,
},
},
}
err := survey.Ask(prompt, &manifest, getIconsOpt(), survey.WithValidator(survey.Required))
// endregion
// region ID
err = survey.AskOne(&survey.Input{
Message: fmt.Sprintf("What's the %s of the package?", style.QuestionElement("ID")),
Default: fmt.Sprintf("%s.%s", manifest.Publisher, manifest.Name),
}, &manifest.ID, getIconsOpt(), survey.WithValidator(survey.Required))
if err != nil {
log.Fatal(err)
}
// endregion
debug.PrintJSON("All licenses", suggestions.LicenseList)
debug.PrintJSON("Repo license", suggestions.License)
// region License
var licenseResponseIndex int
survey.AskOne(&survey.Select{
Message: fmt.Sprintf("Which %s does your project use?", style.QuestionElement("license")),
Options: suggestions.GetLicenseNames(),
Default: 0,
}, &licenseResponseIndex, getIconsOpt(), survey.WithValidator(survey.Required))
selectedLicense := suggestions.LicenseList[licenseResponseIndex]
manifest.License = selectedLicense.GetName()
manifest.LicenseURL = selectedLicense.GetURL()
// endregion
// region Version
survey.AskOne(&survey.Input{
Message: fmt.Sprintf("What's the %s of your project the manifest shall point to?", style.QuestionElement("version")),
Default: suggestions.Version,
}, &manifest.Version, getIconsOpt(), survey.WithValidator(survey.Required))
// endregion
writeManifest(&manifest)
}
func writeManifest(manifest *manifestData) {
log.Printf("Data: %+v\n", &manifest)
yamlContent, err := yaml.Marshal(&manifest)
if err != nil {
log.Fatal(err)
}
log.Println("\nMarshaled yaml", string(yamlContent))
dir := fmt.Sprintf("%s/%s", manifest.Publisher, manifest.Name)
filename := fmt.Sprintf("%s-%s.yaml", manifest.Name, manifest.Version)
fullPath := path.Join(dir, filename)
os.MkdirAll(dir, os.ModePerm)
f, err := os.Create(fullPath)
if err != nil {
log.Fatal("Error in opening file for writing: ", err)
}
_, err = f.Write(yamlContent)
if err != nil {
log.Fatal("Error in writing file: ", err)
}
fmt.Fprintf(color.Output, "The manifest file has been saved to %s", color.GreenString(fullPath))
}
func getIconsOpt() survey.AskOpt {
return survey.WithIcons(func(is *survey.IconSet) {
is.Question.Text = "[?]"
})
}