Skip to content

Commit f2b89e0

Browse files
committed
Add --config (-c) flag to set a path to a valid config file via command line
1 parent 180ac18 commit f2b89e0

File tree

4 files changed

+106
-29
lines changed

4 files changed

+106
-29
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ USAGE:
4242
xplane-gateway-downloader config [command options] [arguments...]
4343
4444
OPTIONS:
45+
--config path, -c path The path to the config.json (default: "config.json")
4546
--custom-scenery-folder path, --csf path The path to CustomScenery folder of x-plane
4647
--x-plane-version version, -v version Set the current version of x-plane
4748
```
@@ -75,7 +76,8 @@ USAGE:
7576
xplane-gateway-downloader install [command options] [arguments...]
7677

7778
OPTIONS:
78-
--icao ICAO, -i ICAO Install an airport by ICAO code
79+
--config path, -c path The path to the config.json (default: "config.json")
80+
--icao ICAO, -i ICAO Install an airport by ICAO code
7981
```
8082
8183
Example: ``xplane-gateway-downloader install --icao EDDF``
@@ -91,7 +93,7 @@ USAGE:
9193
xplane-gateway-downloader update [command options] [arguments...]
9294

9395
OPTIONS:
94-
--help, -h show help (default: false)
96+
--config path, -c path The path to the config.json (default: "config.json")
9597
```
9698
9799
Example: ``xplane-gateway-downloader update``
@@ -107,7 +109,8 @@ USAGE:
107109
xplane-gateway-downloader uninstall [command options] [arguments...]
108110

109111
OPTIONS:
110-
--icao ICAO, -i ICAO Uninstall an airport by ICAO code
112+
--config path, -c path The path to the config.json (default: "config.json")
113+
--icao ICAO, -i ICAO Uninstall an airport by ICAO code
111114
```
112115
113116
Example: ``xplane-gateway-downloader uninstall --icao EDDF``

app/action.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package app
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/urfave/cli/v2"
7+
"github.com/xEtarusx/xplane-gateway-downloader/config"
8+
"log"
9+
"os"
10+
)
11+
12+
type Action struct {
13+
}
14+
15+
func (a *Action) Process(c *cli.Context, callback func(c *cli.Context) error) error {
16+
configFileLocation := c.String("config")
17+
18+
// Config file not found
19+
if _, err := os.Stat(configFileLocation); errors.Is(err, os.ErrNotExist) {
20+
fmt.Printf("Config file '%s' not found. Download the default config.json from the project and put it next to the binary.", configFileLocation)
21+
return nil
22+
}
23+
24+
// Load config.json
25+
cfg, err := config.LoadConfig(configFileLocation)
26+
if err != nil {
27+
log.Println(err)
28+
return nil
29+
}
30+
31+
// Save config.json content into GlobalConfig
32+
config.GlobalConfig = cfg
33+
34+
// Call the callback function for all logic
35+
err = callback(c)
36+
if err != nil {
37+
log.Println(err)
38+
return nil
39+
}
40+
41+
// Save GlobalConfig back into config.json
42+
err = config.SaveConfig(configFileLocation)
43+
if err != nil {
44+
log.Println(err)
45+
return nil
46+
}
47+
48+
return nil
49+
}

app/uninstall.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func ActionUninstall(c *cli.Context) error {
1919

2020
// Check if airport is not installed locally
2121
if !config.GlobalConfig.IsAirportInstalled(icao) {
22-
fmt.Printf("Airport %s is not installed", icao)
22+
fmt.Printf("%s is currently not installed", icao)
2323
return nil
2424
}
2525

@@ -32,5 +32,7 @@ func ActionUninstall(c *cli.Context) error {
3232
// delete airport from local config
3333
config.GlobalConfig.AirportConfig = config.GlobalConfig.RemoveAirport(icao)
3434

35+
fmt.Printf("%s was successfully uninstalled\n", icao)
36+
3537
return nil
3638
}

main.go

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ package main
33
import (
44
"github.com/urfave/cli/v2"
55
"github.com/xEtarusx/xplane-gateway-downloader/app"
6-
"github.com/xEtarusx/xplane-gateway-downloader/config"
76
"log"
87
"os"
98
)
109

1110
func main() {
12-
cfg, err := config.LoadConfig("config.json")
13-
if err != nil {
14-
log.Fatal(err)
15-
}
16-
17-
config.GlobalConfig = cfg
11+
action := app.Action{}
1812

1913
a := &cli.App{
2014
Name: "X-Plane Gateway Downloader",
@@ -23,10 +17,18 @@ func main() {
2317
Description: "Update airport sceneries from the X-Plane Gateway",
2418
Commands: []*cli.Command{
2519
{
26-
Name: "install",
27-
Usage: "Install a new airport scenery pack",
28-
Action: app.ActionInstall,
20+
Name: "install",
21+
Usage: "Install a new airport scenery pack",
22+
Action: func(c *cli.Context) error {
23+
return action.Process(c, app.ActionInstall)
24+
},
2925
Flags: []cli.Flag{
26+
&cli.StringFlag{
27+
Name: "config",
28+
Aliases: []string{"c"},
29+
Usage: "The `path` to the config.json",
30+
Value: "config.json",
31+
},
3032
&cli.StringFlag{
3133
Name: "icao",
3234
Aliases: []string{"i"},
@@ -35,15 +37,33 @@ func main() {
3537
},
3638
},
3739
{
38-
Name: "update",
39-
Usage: "Update all installed airport scenery packs",
40-
Action: app.ActionUpdate,
40+
Name: "update",
41+
Usage: "Update all installed airport scenery packs",
42+
Action: func(c *cli.Context) error {
43+
return action.Process(c, app.ActionUpdate)
44+
},
45+
Flags: []cli.Flag{
46+
&cli.StringFlag{
47+
Name: "config",
48+
Aliases: []string{"c"},
49+
Usage: "The `path` to the config.json",
50+
Value: "config.json",
51+
},
52+
},
4153
},
4254
{
43-
Name: "uninstall",
44-
Usage: "Uninstall an installed airport scenery pack",
45-
Action: app.ActionUninstall,
55+
Name: "uninstall",
56+
Usage: "Uninstall an installed airport scenery pack",
57+
Action: func(c *cli.Context) error {
58+
return action.Process(c, app.ActionUninstall)
59+
},
4660
Flags: []cli.Flag{
61+
&cli.StringFlag{
62+
Name: "config",
63+
Aliases: []string{"c"},
64+
Usage: "The `path` to the config.json",
65+
Value: "config.json",
66+
},
4767
&cli.StringFlag{
4868
Name: "icao",
4969
Aliases: []string{"i"},
@@ -52,10 +72,18 @@ func main() {
5272
},
5373
},
5474
{
55-
Name: "config",
56-
Usage: "Configure the application",
57-
Action: app.ActionConfig,
75+
Name: "config",
76+
Usage: "Configure the application",
77+
Action: func(c *cli.Context) error {
78+
return action.Process(c, app.ActionConfig)
79+
},
5880
Flags: []cli.Flag{
81+
&cli.StringFlag{
82+
Name: "config",
83+
Aliases: []string{"c"},
84+
Usage: "The `path` to the config.json",
85+
Value: "config.json",
86+
},
5987
&cli.StringFlag{
6088
Name: "custom-scenery-folder",
6189
Aliases: []string{"csf"},
@@ -71,12 +99,7 @@ func main() {
7199
},
72100
}
73101

74-
err = a.Run(os.Args)
75-
if err != nil {
76-
log.Fatal(err)
77-
}
78-
79-
err = config.SaveConfig("config.json")
102+
err := a.Run(os.Args)
80103
if err != nil {
81104
log.Fatal(err)
82105
}

0 commit comments

Comments
 (0)