Instead of using command-line flags, I decided to generate the configs if they don't exist
This commit is contained in:
24
main.go
24
main.go
@@ -2,9 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,12 +33,12 @@ func mustExist(filename string) {
|
|||||||
// It returns two values: the first is true if the config file exists.
|
// It returns two values: the first is true if the config file exists.
|
||||||
// If it does, the second value is the config filename.
|
// If it does, the second value is the config filename.
|
||||||
// If it doesn't, the second value is blank and can be ignored.
|
// If it doesn't, the second value is blank and can be ignored.
|
||||||
func getConfig(extension string) (bool, string) {
|
func getConfig(configPath, extension string) (bool, string) {
|
||||||
if extension == "" {
|
if extension == "" {
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
// Assuming the file has an extension
|
// Assuming the file has an extension
|
||||||
fileName := "config/" + extension[1:] + ".conf"
|
fileName := filepath.Join(configPath, extension[1:]+".conf")
|
||||||
if exists := fileExists(fileName); exists == false {
|
if exists := fileExists(fileName); exists == false {
|
||||||
return false, ""
|
return false, ""
|
||||||
} else {
|
} else {
|
||||||
@@ -60,11 +60,19 @@ func printFile(fileName string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
generateConfigFlag := flag.Bool("generate-config", false, "Generate default config files")
|
// Check if config exists. If it doesn't, generate the config files.
|
||||||
flag.Parse()
|
var configPath string // Location of config files, depends on OS
|
||||||
if *generateConfigFlag {
|
if runningOnWindows() {
|
||||||
|
configPath = "%APPDATA%\\ccat"
|
||||||
|
} else {
|
||||||
|
currentUser, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
configPath = filepath.Join("/home/" + currentUser.Username + "/.config/ccat/")
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||||
generateDefaultConfigs()
|
generateDefaultConfigs()
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if user has provided a file name
|
// Check if user has provided a file name
|
||||||
@@ -75,7 +83,7 @@ func main() {
|
|||||||
mustExist(fileName)
|
mustExist(fileName)
|
||||||
|
|
||||||
extension := filepath.Ext(fileName)
|
extension := filepath.Ext(fileName)
|
||||||
configExists, configFilename := getConfig(extension)
|
configExists, configFilename := getConfig(configPath, extension)
|
||||||
// If the given file has no corresponding config, print it out
|
// If the given file has no corresponding config, print it out
|
||||||
// and exit.
|
// and exit.
|
||||||
if configExists == false {
|
if configExists == false {
|
||||||
|
Reference in New Issue
Block a user