All checks were successful
Build (artifact) / build (push) Has been skipped
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func createFile(fileName string, filePermission os.FileMode, data []byte) {
|
|
fileInfo, err := os.Stat(fileName)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
file, err := os.Create(fileName)
|
|
if err != nil {
|
|
fmt.Println("Error creating configuration file: ", err)
|
|
os.Exit(1)
|
|
}
|
|
defer file.Close()
|
|
|
|
_, err = file.Write(data)
|
|
if err != nil {
|
|
fmt.Println("Error writing to file: ", err)
|
|
os.Exit(1)
|
|
}
|
|
err = file.Chmod(filePermission)
|
|
if err != nil {
|
|
fmt.Println("Error changing file permission: ", err)
|
|
}
|
|
} else {
|
|
fmt.Println("Error opening configuration file: ", err)
|
|
os.Exit(1)
|
|
}
|
|
} else {
|
|
if fileInfo.Size() == 0 {
|
|
file, err := os.Create(fileName)
|
|
if err != nil {
|
|
fmt.Println("Error creating configuration file: ", err)
|
|
os.Exit(1)
|
|
}
|
|
defer file.Close()
|
|
|
|
_, err = file.Write(data)
|
|
if err != nil {
|
|
fmt.Println("Error writing to file:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func basicCmd(use, short string, commandFunc func(cmd *cobra.Command, args []string)) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: use,
|
|
Short: short,
|
|
Run: commandFunc,
|
|
}
|
|
}
|