-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from simulot/simulot/issue189
### Improvement: [#189](#189) Use a configuration file to store server's address and its API key The server URL and the API key are now stored into a configuration file (by default $HOME/.immich-go/immich-go.json). If not provided in the CLI argument, those values are read from the configuration file. The option `-use-configuration=path/to/config/file` let you specify the configuration file. ### fix: [#193](#193) Flags not being passed to subcommands #193
- Loading branch information
Showing
6 changed files
with
120 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package configuration | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type Configuration struct { | ||
APIURL string `json:",omitempty"` | ||
ServerURL string `json:",omitempty"` | ||
APIKey string | ||
} | ||
|
||
// DefaultFile return the default configuration file name | ||
// Return a local file nama when the default UserHomeDir can't be determined, | ||
func DefaultFile() string { | ||
d, err := os.UserHomeDir() | ||
if err != nil { | ||
return "immich-go.json" | ||
} | ||
return filepath.Join(d, ".immich-go", "immich-go.json") | ||
} | ||
|
||
// Read the configuration in file name | ||
func Read(name string) (Configuration, error) { | ||
f, err := os.Open(name) | ||
if err != nil { | ||
return Configuration{}, err | ||
} | ||
defer f.Close() | ||
var c Configuration | ||
err = json.NewDecoder(f).Decode(&c) | ||
if err != nil { | ||
return Configuration{}, err | ||
} | ||
return c, nil | ||
} | ||
|
||
// Write the configuration in the file name | ||
// Create the needed sub directories as needed | ||
func (c Configuration) Write(name string) error { | ||
d, _ := filepath.Split(name) | ||
if d != "" { | ||
err := os.MkdirAll(d, 0o700) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
f, err := os.OpenFile(name, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o700) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
enc := json.NewEncoder(f) | ||
enc.SetIndent("", " ") | ||
return enc.Encode(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
This package handles the configuration file stored into user's machine profile | ||
*/ | ||
package configuration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters