|
|
@ -4,13 +4,14 @@ import ( |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
|
|
|
|
"path/filepath" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"twitch-hooks/discordhooks" |
|
|
|
"twitch-hooks/discordhooks" |
|
|
|
"twitch-hooks/twitchhooks" |
|
|
|
"twitch-hooks/twitchhooks" |
|
|
|
"twitch-hooks/vkhooks" |
|
|
|
"twitch-hooks/vkhooks" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const configFilename string = "config.cfg" |
|
|
|
const DefaultConfigFilename string = "config.cfg" |
|
|
|
|
|
|
|
|
|
|
|
type keys struct { |
|
|
|
type keys struct { |
|
|
|
Twitch twitchhooks.Keys |
|
|
|
Twitch twitchhooks.Keys |
|
|
@ -30,22 +31,23 @@ type Config struct { |
|
|
|
Messages messages |
|
|
|
Messages messages |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Checks if config file exists in the same directory
|
|
|
|
// Checks if config file exists
|
|
|
|
func ConfigExists() bool { |
|
|
|
func ConfigExists(configPath string) bool { |
|
|
|
_, err := os.Stat(configFilename) |
|
|
|
_, err := os.Stat(configPath) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Creates a new config file in current directory.
|
|
|
|
// Creates a new config file in specified directory
|
|
|
|
func CreateConfig() error { |
|
|
|
func CreateConfig(dir string) error { |
|
|
|
// create a config file in the same directory
|
|
|
|
// create a config file in the same directory
|
|
|
|
configF, err := os.Create(configFilename) |
|
|
|
configF, err := os.Create(filepath.Join(dir, DefaultConfigFilename)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("could not create a config file: %s", err) |
|
|
|
return fmt.Errorf("could not create a config file: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
defer configF.Close() |
|
|
|
|
|
|
|
|
|
|
|
// write default config fields
|
|
|
|
// write default config fields
|
|
|
|
defaults, err := json.MarshalIndent(&Config{}, "", " ") |
|
|
|
defaults, err := json.MarshalIndent(&Config{}, "", " ") |
|
|
@ -63,9 +65,9 @@ func CreateConfig() error { |
|
|
|
// Opens and reads config file, returns `Config` struct.
|
|
|
|
// Opens and reads config file, returns `Config` struct.
|
|
|
|
// If ReadConfig cannot unmarshal config file - it creates a new one with
|
|
|
|
// If ReadConfig cannot unmarshal config file - it creates a new one with
|
|
|
|
// all default fields
|
|
|
|
// all default fields
|
|
|
|
func ReadConfig() (*Config, error) { |
|
|
|
func ReadConfig(pathToConfig string) (*Config, error) { |
|
|
|
// get config`s contents
|
|
|
|
// get config`s contents
|
|
|
|
configContents, err := os.ReadFile(configFilename) |
|
|
|
configContents, err := os.ReadFile(pathToConfig) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, fmt.Errorf("could not read config: %s", err) |
|
|
|
return nil, fmt.Errorf("could not read config: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
@ -73,7 +75,7 @@ func ReadConfig() (*Config, error) { |
|
|
|
var config Config |
|
|
|
var config Config |
|
|
|
err = json.Unmarshal(configContents, &config) |
|
|
|
err = json.Unmarshal(configContents, &config) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
_ = CreateConfig() |
|
|
|
_ = CreateConfig(filepath.Dir(pathToConfig)) |
|
|
|
return nil, fmt.Errorf("could not unmarshal config: %s\nCreatead a new one", err) |
|
|
|
return nil, fmt.Errorf("could not unmarshal config: %s\nCreatead a new one", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|