|
| 1 | +package slacker |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | +) |
| 9 | + |
| 10 | +// Config is config for slack |
| 11 | +type Config struct { |
| 12 | + // URL is incoming url by slack |
| 13 | + URL string `json:"url"` |
| 14 | +} |
| 15 | + |
| 16 | +var ( |
| 17 | + h = os.Getenv("HOME") |
| 18 | + confDirName = ".slack_cli" |
| 19 | + confFileName = "conf.json" |
| 20 | + confDir = fmt.Sprintf("%s/%s", h, confDirName) |
| 21 | + confPath = fmt.Sprintf("%s/%s", confDir, confFileName) |
| 22 | +) |
| 23 | + |
| 24 | +// ExistConfig is check conf finle exists |
| 25 | +func ExistConfig() bool { |
| 26 | + _, err := os.Stat(confPath) |
| 27 | + return err == nil |
| 28 | +} |
| 29 | + |
| 30 | +// CreateConfig is initialize conf file |
| 31 | +func CreateConfig() (err error) { |
| 32 | + err = os.MkdirAll(confDir, 0755) |
| 33 | + if err != nil { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + _, err = os.Create(confPath) |
| 38 | + return |
| 39 | +} |
| 40 | + |
| 41 | +// ReadConfig is read conf file |
| 42 | +func ReadConfig() (c *Config, err error) { |
| 43 | + b, err := ioutil.ReadFile(confPath) |
| 44 | + if err != nil { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + c = new(Config) |
| 49 | + err = json.Unmarshal(b, c) |
| 50 | + return |
| 51 | +} |
| 52 | + |
| 53 | +// WriteConfig is write to conf file |
| 54 | +func WriteConfig(c *Config) (err error) { |
| 55 | + b, err := json.Marshal(c) |
| 56 | + if err != nil { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + err = ioutil.WriteFile(confPath, b, 0755) |
| 61 | + return |
| 62 | +} |
| 63 | + |
| 64 | +// RemoveConfig is remove conf file |
| 65 | +func RemoveConfig() (err error) { |
| 66 | + err = os.RemoveAll(confPath) |
| 67 | + return |
| 68 | +} |
0 commit comments