ignore
1package cmd
2
3import (
4"fmt"
5"github.com/neptunsk1y/ignore/internal"
6"github.com/spf13/cobra"
7"log"
8"os"
9)
10
11var addCommand = &cobra.Command{
12Use: "add [filename] [template]",
13Short: "Add a template to .ignore file",
14Run: func(cmd *cobra.Command, args []string) {
15pathFile := "./." + args[0] + "ignore"
16_, err := os.Stat(pathFile)
17if err != nil {
18if os.IsNotExist(err) {
19log.Fatal("The file does not exist")
20} else {
21log.Fatal("Error:", err)
22}
23}
24
25tr := internal.NewTemplateRegistry()
26template := args[1]
27if !tr.HasTemplate(template) {
28log.Fatal("template does not exist")
29}
30
31file, err := os.OpenFile(pathFile, os.O_APPEND|os.O_WRONLY, 0666)
32if err != nil {
33log.Fatal(err)
34}
35defer file.Close()
36
37err = tr.CopyTemplate(template, file)
38if err != nil {
39log.Fatal(err)
40}
41fmt.Printf("%s template has been added to .%signore\n", template, args[0])
42},
43}
44
45func init() {
46rootCmd.AddCommand(addCommand)
47}
48