Skip to content

Commit d0a864d

Browse files
committed
parameters
1 parent 5821c71 commit d0a864d

File tree

9 files changed

+323
-196
lines changed

9 files changed

+323
-196
lines changed

Gopkg.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/config/flag.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package config
22

33
import (
4+
"fmt"
45
"github.com/spf13/cobra"
6+
"jrs/config"
57
)
68

79
var (
810
url string
911
api string
1012
)
1113

12-
var config = &cobra.Command{
14+
var Config = &cobra.Command{
1315
Use: "config",
1416
}
1517

@@ -31,12 +33,16 @@ var jackett = &cobra.Command{
3133

3234
var save = &cobra.Command{
3335
Use: "save",
36+
Run: func(cmd *cobra.Command, args []string) {
37+
fmt.Println(args[0])
38+
config.Params.SaveFile(args[0])
39+
},
3440
}
3541

3642
func init() {
3743
radarr.AddCommand(save)
3844
sonarr.AddCommand(save)
3945
jackett.AddCommand(save)
4046
set.AddCommand(radarr, sonarr, jackett)
41-
config.AddCommand(set)
47+
Config.AddCommand(set)
4248
}

cmd/flag.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package cmd
22

33
import (
4-
"flag"
54
"github.com/spf13/cobra"
5+
cfg "jrs/cmd/config"
6+
"jrs/cmd/jackett"
7+
"jrs/cmd/radarr"
8+
"jrs/cmd/sonarr"
9+
"jrs/config"
610
)
711

8-
func ParseOptions() {
9-
var configPath string
10-
flag.StringVar(&configPath, "config", "config.toml", "Config file path")
11-
12-
}
13-
14-
var RadarrCmd = &cobra.Command{
15-
Use: "radarr",
16-
Short: "Radarr commands",
12+
var RootCmd = &cobra.Command{Use: "jrs",
13+
Run: func(cmd *cobra.Command, args []string) {
14+
},
1715
}
1816

19-
var RootCmd = &cobra.Command{Use: "jrs"}
20-
2117
func init() {
22-
RootCmd.AddCommand(RadarrCmd)
18+
RootCmd.PersistentFlags().StringVarP(&config.ConfPath, "config", "c", "config.toml", "Config file path")
19+
config.ParseConfigFile()
20+
21+
RootCmd.AddCommand(radarr.Cmd)
22+
RootCmd.AddCommand(sonarr.Cmd)
23+
RootCmd.AddCommand(jackett.Cmd)
24+
RootCmd.AddCommand(cfg.Config)
2325
}

cmd/jackett/flag.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package jackett
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var Cmd = &cobra.Command{
8+
Use: "jackett",
9+
Short: "Jackett commands",
10+
}
11+
12+
func init() {
13+
Cmd.Flags().StringP("url", "u", "http://localhost:9117", "Sonarr URL")
14+
Cmd.Flags().StringP("api", "a", "", "API Key")
15+
}

cmd/radarr/flag.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package radarr
22

33
import (
4-
"flag"
4+
"fmt"
55
"github.com/spf13/cobra"
66
)
77

8-
func ParseOptions() {
9-
var configPath string
10-
flag.StringVar(&configPath, "config", "config.toml", "Config file path")
11-
12-
}
13-
14-
var RadarrCmd = &cobra.Command{
8+
var Cmd = &cobra.Command{
159
Use: "radarr",
1610
Short: "Radarr commands",
11+
Run: func(cmd *cobra.Command, args []string) {
12+
fmt.Println(cmd.Flag("config").Value)
13+
},
1714
}
1815

19-
var RootCmd = &cobra.Command{Use: "jrs"}
20-
2116
func init() {
22-
RootCmd.AddCommand(RadarrCmd)
17+
// url := Cmd.Flags().StringP("url", "u", "http://localhost:7878", "Radarr URL")
18+
// api := Cmd.Flags().StringP("api", "a", "", "API Key")
19+
//
20+
// if *url != "" {
21+
// matched := utils.SplitUrl(*url)
22+
//
23+
// }
2324
}

cmd/sonarr/flag.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package sonarr
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var Cmd = &cobra.Command{
8+
Use: "sonarr",
9+
Short: "Sonarr commands",
10+
}
11+
12+
func init() {
13+
Cmd.Flags().StringP("url", "u", "http://localhost:8989", "Sonarr URL")
14+
Cmd.Flags().StringP("api", "a", "", "API Key")
15+
}

config/config.go

Lines changed: 94 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package config
22

3-
import "strings"
3+
import (
4+
"github.com/burntsushi/toml"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
"reflect"
9+
"strings"
10+
)
11+
12+
var (
13+
ConfPath = "config.toml"
14+
Params *Config
15+
)
416

517
type Config struct {
6-
Jackett Jackett
7-
Dest []Destination `toml:"destinations"`
18+
Dest []Destination `toml:"destinations"`
819
}
920

1021
type Destination struct {
1122
Name string
12-
IP string
13-
Port int
14-
API string
15-
}
16-
17-
type Jackett struct {
18-
IP string
19-
Port int
20-
API string
23+
Ip string
24+
Port uint16
25+
Api string
2126
}
2227

2328
func (c *Config) GetDestination(name string) *Destination {
@@ -29,4 +34,80 @@ func (c *Config) GetDestination(name string) *Destination {
2934
return nil
3035
}
3136

32-
var Params *Config
37+
func (c *Config) SaveFile(path string) error {
38+
ext := filepath.Ext(path)
39+
if ext == "" {
40+
path = path + ".toml"
41+
} else if ext != ".toml" {
42+
log.Fatal("Config file does not have toml extension, please correct it")
43+
}
44+
45+
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
46+
47+
defer f.Close()
48+
if err != nil {
49+
return err
50+
}
51+
52+
if err := toml.NewEncoder(f).Encode(c); err != nil {
53+
log.Fatal(err)
54+
return err
55+
}
56+
57+
return nil
58+
}
59+
60+
func (c *Config) ChangeParams(dest, param string, value interface{}) {
61+
main := reflect.ValueOf(c)
62+
mainElem := main.Elem()
63+
items := mainElem.FieldByName("Dest")
64+
65+
for i := 0; i < items.Len(); i++ {
66+
item := items.Index(i)
67+
if item.FieldByName("Name").String() == strings.Title(dest) {
68+
field := item.FieldByName(strings.Title(param))
69+
if field.IsValid() {
70+
if field.CanSet() {
71+
if field.Kind() == reflect.Uint16 {
72+
if reflect.TypeOf(value).String() == "int" {
73+
newValue := uint64(value.(int))
74+
field.SetUint(newValue)
75+
}
76+
}
77+
if field.Kind() == reflect.String {
78+
newValue := value.(string)
79+
field.SetString(newValue)
80+
}
81+
}
82+
}
83+
84+
}
85+
}
86+
}
87+
88+
func ParseConfigFile() {
89+
if _, err := toml.DecodeFile(ConfPath, &Params); err != nil {
90+
log.Fatal(err)
91+
}
92+
93+
if len(Params.Dest) < 1 {
94+
log.Fatal("There is no Destination configured.")
95+
}
96+
97+
for _, dest := range Params.Dest {
98+
if dest.Api == "" {
99+
param := os.Getenv(strings.ToUpper(dest.Name))
100+
101+
if param != "" {
102+
Params.GetDestination(dest.Name).Api = param
103+
} else {
104+
if len(Params.Dest) == 1 {
105+
log.Fatalf("There is no %s API configured.", dest.Name)
106+
} else {
107+
log.Printf("There is no %s API configured.", dest.Name)
108+
}
109+
}
110+
}
111+
112+
}
113+
}

0 commit comments

Comments
 (0)