-
Notifications
You must be signed in to change notification settings - Fork 1
/
new_command.go
83 lines (66 loc) · 1.53 KB
/
new_command.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
// Copyright 2019 Axel Etcheverry. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package commands
import (
"fmt"
"io/ioutil"
"time"
"github.com/euskadi31/license/pkg/license/template"
"github.com/AlecAivazis/survey/v2"
"github.com/euskadi31/license/pkg/license/identifier"
"github.com/urfave/cli"
)
// NewCommand constructor
func NewCommand() cli.Command {
return cli.Command{
Name: "new",
Usage: "Generate a license file",
Flags: []cli.Flag{
cli.StringFlag{
Name: "format, f",
Value: "",
Usage: "license format (md)",
},
},
Action: newAction,
}
}
func newAction(c *cli.Context) error {
ext := c.String("format")
context := &template.Context{
Year: time.Now().Format("2006"),
}
id := c.Args().First()
if id == "" {
prompt := &survey.Select{
Message: "Choose a license:",
Options: identifier.List(),
Default: "",
}
if err := survey.AskOne(prompt, &id); err != nil {
return err
}
}
if context.CopyrightHolders == "" {
prompt := &survey.Input{
Message: "Choose a copyright holders:",
Help: "ACME Inc.",
}
if err := survey.AskOne(prompt, &context.CopyrightHolders); err != nil {
return err
}
}
content, err := template.Generate(id, context)
if err != nil {
return err
}
if ext != "" {
ext = "." + ext
}
if err := ioutil.WriteFile("LICENSE"+ext, content, 0644); err != nil {
return err
}
fmt.Fprintf(c.App.Writer, "LICENSE file created with %s license\n", id)
return nil
}