diff --git a/README.md b/README.md index b2172f0..b4fe5ed 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ USAGE: xplane-gateway-downloader config [command options] [arguments...] OPTIONS: + --config path, -c path The path to the config.json (default: "config.json") --custom-scenery-folder path, --csf path The path to CustomScenery folder of x-plane --x-plane-version version, -v version Set the current version of x-plane ``` @@ -75,7 +76,8 @@ USAGE: xplane-gateway-downloader install [command options] [arguments...] OPTIONS: - --icao ICAO, -i ICAO Install an airport by ICAO code + --config path, -c path The path to the config.json (default: "config.json") + --icao ICAO, -i ICAO Install an airport by ICAO code ``` Example: ``xplane-gateway-downloader install --icao EDDF`` @@ -91,7 +93,7 @@ USAGE: xplane-gateway-downloader update [command options] [arguments...] OPTIONS: - --help, -h show help (default: false) + --config path, -c path The path to the config.json (default: "config.json") ``` Example: ``xplane-gateway-downloader update`` @@ -107,7 +109,8 @@ USAGE: xplane-gateway-downloader uninstall [command options] [arguments...] OPTIONS: - --icao ICAO, -i ICAO Uninstall an airport by ICAO code + --config path, -c path The path to the config.json (default: "config.json") + --icao ICAO, -i ICAO Uninstall an airport by ICAO code ``` Example: ``xplane-gateway-downloader uninstall --icao EDDF`` \ No newline at end of file diff --git a/app/action.go b/app/action.go new file mode 100644 index 0000000..5d1d22e --- /dev/null +++ b/app/action.go @@ -0,0 +1,49 @@ +package app + +import ( + "errors" + "fmt" + "github.com/urfave/cli/v2" + "github.com/xEtarusx/xplane-gateway-downloader/config" + "log" + "os" +) + +type Action struct { +} + +func (a *Action) Process(c *cli.Context, callback func(c *cli.Context) error) error { + configFileLocation := c.String("config") + + // Config file not found + if _, err := os.Stat(configFileLocation); errors.Is(err, os.ErrNotExist) { + fmt.Printf("Config file '%s' not found. Download the default config.json from the project and put it next to the binary.", configFileLocation) + return nil + } + + // Load config.json + cfg, err := config.LoadConfig(configFileLocation) + if err != nil { + log.Println(err) + return nil + } + + // Save config.json content into GlobalConfig + config.GlobalConfig = cfg + + // Call the callback function for all logic + err = callback(c) + if err != nil { + log.Println(err) + return nil + } + + // Save GlobalConfig back into config.json + err = config.SaveConfig(configFileLocation) + if err != nil { + log.Println(err) + return nil + } + + return nil +} diff --git a/app/uninstall.go b/app/uninstall.go index 8ee4d91..5cb4598 100644 --- a/app/uninstall.go +++ b/app/uninstall.go @@ -19,7 +19,7 @@ func ActionUninstall(c *cli.Context) error { // Check if airport is not installed locally if !config.GlobalConfig.IsAirportInstalled(icao) { - fmt.Printf("Airport %s is not installed", icao) + fmt.Printf("%s is currently not installed", icao) return nil } @@ -32,5 +32,7 @@ func ActionUninstall(c *cli.Context) error { // delete airport from local config config.GlobalConfig.AirportConfig = config.GlobalConfig.RemoveAirport(icao) + fmt.Printf("%s was successfully uninstalled\n", icao) + return nil } diff --git a/main.go b/main.go index 19f0a8a..51365c9 100644 --- a/main.go +++ b/main.go @@ -3,18 +3,12 @@ package main import ( "github.com/urfave/cli/v2" "github.com/xEtarusx/xplane-gateway-downloader/app" - "github.com/xEtarusx/xplane-gateway-downloader/config" "log" "os" ) func main() { - cfg, err := config.LoadConfig("config.json") - if err != nil { - log.Fatal(err) - } - - config.GlobalConfig = cfg + action := app.Action{} a := &cli.App{ Name: "X-Plane Gateway Downloader", @@ -23,10 +17,18 @@ func main() { Description: "Update airport sceneries from the X-Plane Gateway", Commands: []*cli.Command{ { - Name: "install", - Usage: "Install a new airport scenery pack", - Action: app.ActionInstall, + Name: "install", + Usage: "Install a new airport scenery pack", + Action: func(c *cli.Context) error { + return action.Process(c, app.ActionInstall) + }, Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Aliases: []string{"c"}, + Usage: "The `path` to the config.json", + Value: "config.json", + }, &cli.StringFlag{ Name: "icao", Aliases: []string{"i"}, @@ -35,15 +37,33 @@ func main() { }, }, { - Name: "update", - Usage: "Update all installed airport scenery packs", - Action: app.ActionUpdate, + Name: "update", + Usage: "Update all installed airport scenery packs", + Action: func(c *cli.Context) error { + return action.Process(c, app.ActionUpdate) + }, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Aliases: []string{"c"}, + Usage: "The `path` to the config.json", + Value: "config.json", + }, + }, }, { - Name: "uninstall", - Usage: "Uninstall an installed airport scenery pack", - Action: app.ActionUninstall, + Name: "uninstall", + Usage: "Uninstall an installed airport scenery pack", + Action: func(c *cli.Context) error { + return action.Process(c, app.ActionUninstall) + }, Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Aliases: []string{"c"}, + Usage: "The `path` to the config.json", + Value: "config.json", + }, &cli.StringFlag{ Name: "icao", Aliases: []string{"i"}, @@ -52,10 +72,18 @@ func main() { }, }, { - Name: "config", - Usage: "Configure the application", - Action: app.ActionConfig, + Name: "config", + Usage: "Configure the application", + Action: func(c *cli.Context) error { + return action.Process(c, app.ActionConfig) + }, Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Aliases: []string{"c"}, + Usage: "The `path` to the config.json", + Value: "config.json", + }, &cli.StringFlag{ Name: "custom-scenery-folder", Aliases: []string{"csf"}, @@ -71,12 +99,7 @@ func main() { }, } - err = a.Run(os.Args) - if err != nil { - log.Fatal(err) - } - - err = config.SaveConfig("config.json") + err := a.Run(os.Args) if err != nil { log.Fatal(err) }