Skip to content

Commit

Permalink
update config params
Browse files Browse the repository at this point in the history
  • Loading branch information
zan8in committed Mar 16, 2024
1 parent 85d98d8 commit f69e050
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
58 changes: 43 additions & 15 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/zan8in/afrog/v3/pkg/utils"
Expand Down Expand Up @@ -88,8 +89,11 @@ type Cyberspace struct {
const afrogConfigFilename = "afrog-config.yaml"

// Create and initialize afrog-config.yaml configuration info
func NewConfig() (*Config, error) {
if isExistConfigFile() != nil {
func NewConfig(configFile string) (*Config, error) {
if len(configFile) > 0 && !strings.HasSuffix(configFile, ".yml") && !strings.HasSuffix(configFile, ".yaml") {
return nil, errors.New("afrog config file must be yaml format")
}
if isExistConfigFile(configFile) != nil {
c := Config{}
c.ServerAddress = ":16868"

Expand Down Expand Up @@ -134,18 +138,25 @@ func NewConfig() (*Config, error) {
cyberspace.ZoomEyes = []string{""}
c.Cyberspace = cyberspace

WriteConfiguration(&c)
WriteConfiguration(&c, configFile)
}
return ReadConfiguration()
return ReadConfiguration(configFile)
}

func isExistConfigFile() error {
func isExistConfigFile(configFile string) error {
if len(configFile) > 0 {
if utils.Exists(configFile) {
return nil
}
return errors.New("could not get config file")
}

homeDir, err := os.UserHomeDir()
if err != nil {
return errors.Wrap(err, "could not get home directory")
}

configFile := filepath.Join(homeDir, ".config", "afrog", afrogConfigFilename)
configFile = filepath.Join(homeDir, ".config", "afrog", afrogConfigFilename)
if utils.Exists(configFile) {
return nil
}
Expand Down Expand Up @@ -180,10 +191,16 @@ func getConfigFile() (string, error) {
}

// ReadConfiguration reads the afrog configuration file from disk.
func ReadConfiguration() (*Config, error) {
afrogConfigFile, err := getConfigFile()
if err != nil {
return nil, err
func ReadConfiguration(configFile string) (*Config, error) {
var afrogConfigFile string
var err error
if len(configFile) > 0 {
afrogConfigFile = configFile
} else {
afrogConfigFile, err = getConfigFile()
if err != nil {
return nil, err
}
}

file, err := os.Open(afrogConfigFile)
Expand All @@ -200,17 +217,28 @@ func ReadConfiguration() (*Config, error) {
}

// WriteConfiguration writes the updated afrog configuration to disk
func WriteConfiguration(config *Config) error {
afrogConfigYAML, err := yaml.Marshal(&config)
if err != nil {
return err
func WriteConfiguration(config *Config, configFile string) error {
var afrogConfigFile string
var err error
if len(configFile) > 0 {
afrogConfigFile = configFile
} else {
afrogConfigFile, err = getConfigFile()
if err != nil {
return err
}
}

afrogConfigFile, err := getConfigFile()
afrogConfigYAML, err := yaml.Marshal(&config)
if err != nil {
return err
}

// afrogConfigFile, err = getConfigFile()
// if err != nil {
// return err
// }

file, err := os.OpenFile(afrogConfigFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
Expand Down
9 changes: 8 additions & 1 deletion pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ type Options struct {
OOBKey string
OOBDomain string
OOBApiUrl string

// path to the afrog configuration file
ConfigFile string
}

func NewOptions() (*Options, error) {
Expand Down Expand Up @@ -255,6 +258,10 @@ func NewOptions() (*Options, error) {
flagSet.BoolVar(&options.Dingtalk, "dingtalk", false, "Start a dingtalk webhook."),
)

flagSet.CreateGroup("configurations", "Configurations",
flagSet.StringVar(&options.ConfigFile, "config", "", "path to the afrog configuration file"),
)

_ = flagSet.Parse()

if err := options.VerifyOptions(); err != nil {
Expand All @@ -279,7 +286,7 @@ func (opt *Options) VerifyOptions() error {
}
}

config, err := NewConfig()
config, err := NewConfig(opt.ConfigFile)
if err != nil {
return err
}
Expand Down

0 comments on commit f69e050

Please sign in to comment.