Skip to content

Commit

Permalink
Add --config (-c) flag to set a path to a valid config file via comma…
Browse files Browse the repository at this point in the history
…nd line
  • Loading branch information
xEtarusx committed Jun 13, 2022
1 parent 180ac18 commit f2b89e0
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 29 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -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``
Expand All @@ -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``
Expand All @@ -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``
49 changes: 49 additions & 0 deletions app/action.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion app/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
73 changes: 48 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"},
Expand All @@ -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"},
Expand All @@ -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"},
Expand All @@ -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)
}
Expand Down

0 comments on commit f2b89e0

Please sign in to comment.