Skip to content

Commit

Permalink
fix: create config dir if not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
oclaussen committed Feb 17, 2021
1 parent 7c34081 commit 1f3c5e0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/chirp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *ClipboardClient) Paste() error {
return fmt.Errorf("could not send paste command: %w", err)
}

fmt.Print(response.Contents)
fmt.Fprint(os.Stdout, response.Contents)

return nil
}
4 changes: 3 additions & 1 deletion pkg/command/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"errors"
"fmt"

"github.com/oclaussen/chirp/pkg/chirp"
Expand Down Expand Up @@ -42,7 +43,8 @@ func withClient(f func(*chirp.ClipboardClient) error) error {
viper.SetConfigName("client")

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
var e *viper.ConfigFileNotFoundError
if errors.As(err, &e) {
log.Warn("no configuration file found: %w")
} else {
return fmt.Errorf("could not read config file: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/command/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func NewConfigureCommand() *cobra.Command {
func Configure() error {
appdir := filepath.Join(os.Getenv("HOME"), fmt.Sprintf(".%s", name))

if err := os.MkdirAll(appdir, 0600); err != nil {
return fmt.Errorf("could not create config directory: %w", err)
}

u, err := url.Parse(viper.GetString(ConfKeyAddress))
if err != nil {
return fmt.Errorf("invalid address: %w", err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/command/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"errors"
"fmt"

"github.com/oclaussen/chirp/pkg/chirp"
Expand Down Expand Up @@ -30,7 +31,8 @@ func withServer(f func(*chirp.ClipboardServer) error) error {
viper.SetConfigName("server")

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
var e *viper.ConfigFileNotFoundError
if errors.As(err, &e) {
log.Warn("no configuration file found: %w")
} else {
return fmt.Errorf("could not read config file: %w", err)
Expand Down

0 comments on commit 1f3c5e0

Please sign in to comment.