Skip to content

Commit 6356690

Browse files
authored
fix: reuse existing license during init (#79)
1 parent e102508 commit 6356690

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

cmd/init.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,19 @@ var initCmd = &cobra.Command{
2929
Short: "Initialise new theme",
3030
Args: cobra.MaximumNArgs(1),
3131
Run: func(cmd *cobra.Command, args []string) {
32-
fmt.Println("Creating theme files...")
32+
fmt.Println("Initialising theme...")
3333

3434
if err := ensureReadme(); err != nil {
35-
fmt.Fprintf(os.Stderr, "Error creating README: %v\n", err)
35+
fmt.Fprintf(os.Stderr, "Error updating README: %v\n", err)
3636
} else {
37-
fmt.Println("Created README.md")
37+
fmt.Println("Updated README.md")
3838
}
3939

40-
if err := ensureLicense(); err != nil {
41-
fmt.Fprintf(os.Stderr, "Error creating LICENSE: %v\n", err)
42-
} else {
43-
fmt.Println("Created LICENSE")
40+
licenseCreated, err := ensureLicense()
41+
if err != nil {
42+
fmt.Fprintf(os.Stderr, "Error updating LICENSE: %v\n", err)
43+
} else if licenseCreated {
44+
fmt.Println("Updated LICENSE")
4445
}
4546

4647
if len(args) > 0 {
@@ -97,14 +98,14 @@ func ensureReadme() error {
9798
return os.WriteFile(fileName, []byte(contentStr), 0644)
9899
}
99100

100-
func ensureLicense() error {
101+
func ensureLicense() (bool, error) {
101102
fileName, err := findAndNormalizeFile("LICENSE")
102103
if err != nil {
103-
return err
104+
return false, err
104105
}
105106

106107
if existingContent, err := os.ReadFile(fileName); err == nil && len(existingContent) > 0 {
107-
return nil
108+
return false, nil
108109
}
109110

110111
year := time.Now().Year()
@@ -130,7 +131,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
130131
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
131132
SOFTWARE.
132133
`, year)
133-
return os.WriteFile(fileName, []byte(contentStr), 0644)
134+
return true, os.WriteFile(fileName, []byte(contentStr), 0644)
134135
}
135136

136137
func findAndNormalizeFile(targetName string) (string, error) {
@@ -153,7 +154,7 @@ func findAndNormalizeFile(targetName string) (string, error) {
153154
return "", err
154155
}
155156

156-
fmt.Printf("renamed %s to %s\n", actualName, targetName)
157+
fmt.Printf("Renamed %s to %s\n", actualName, targetName)
157158
return targetName, nil
158159
}
159160

0 commit comments

Comments
 (0)