Skip to content

Commit

Permalink
feat: use '~/.config/prm' instead of '~/.prm'
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 16, 2018
1 parent 4905d73 commit dca5948
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 13 deletions.
31 changes: 19 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"path"

"github.com/ldez/prm/local"
"github.com/ldez/prm/types"
Expand All @@ -20,8 +19,6 @@ type Configuration struct {
PullRequests map[string][]types.PullRequest `json:"pull_requests,omitempty"`
}

const defaultFileName = ".prm"

var getPathFunc = GetPath

// RemovePullRequest remove a pull request.
Expand Down Expand Up @@ -114,6 +111,11 @@ func ReadFile() ([]Configuration, error) {
return configs, errMarshal
}

errDir := createDirectory(filePath)
if errDir != nil {
return configs, errDir
}

file, errCreate := os.Create(filePath)
if errCreate != nil {
return configs, errCreate
Expand Down Expand Up @@ -147,7 +149,6 @@ func ReadFile() ([]Configuration, error) {

// Save save the configuration into a file.
func Save(configs []Configuration) error {

filePath, err := getPathFunc()
if err != nil {
return err
Expand All @@ -158,15 +159,21 @@ func Save(configs []Configuration) error {
return err
}

err = createDirectory(filePath)
if err != nil {
return err
}

return ioutil.WriteFile(filePath, confJSON, 0644)
}

// GetPath get the configuration file path.
func GetPath() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
func createDirectory(filePath string) error {
baseDir := path.Dir(filePath)
if _, errDirStat := os.Stat(baseDir); errDirStat != nil {
errDir := os.MkdirAll(baseDir, 0700)
if errDir != nil {
return errDir
}
}

return filepath.Join(usr.HomeDir, defaultFileName), nil
return nil
}
3 changes: 3 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -81,6 +82,8 @@ func TestReadFile_should_return_empty_configuration_list_when_file_not_exist(t *
}()
require.NoError(t, err)

fmt.Println(dir)

// Mock GetPath function
getPathFunc = func() (string, error) {
return path.Join(dir, defaultFileName), nil
Expand Down
86 changes: 86 additions & 0 deletions config/path_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// +build !windows

package config

import (
"io"
"log"
"os"
"os/user"
"path"
"path/filepath"
)

const (
defaultFileName = ".config/prm"
oldDefaultFileName = ".prm"
)

// GetPath get the configuration file path.
func GetPath() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}

pathOne := filepath.Join(usr.HomeDir, oldDefaultFileName)
pathTwo := filepath.Join(usr.HomeDir, defaultFileName)

info, err := os.Stat(pathOne)
if err != nil {
if os.IsNotExist(err) {
return pathTwo, nil
}
return "", err
}

log.Println("WARN: old configuration file detected, migration in progress.")

err = copyConfigFile(pathOne, pathTwo, info)
if err != nil {
return "", err
}

err = os.Remove(pathOne)
if err != nil {
return "", err
}

log.Println("WARN: old configuration file detected, migration done.")

return pathTwo, nil
}

func copyConfigFile(src, dst string, info os.FileInfo) error {
baseDir := path.Dir(dst)
err := os.MkdirAll(baseDir, 0700)
if err != nil {
return err
}

f, err := os.Create(dst)
if err != nil {
return err
}
defer safeClose(f.Close)

if err = os.Chmod(f.Name(), info.Mode()); err != nil {
return err
}

s, err := os.Open(src)
if err != nil {
return err
}
defer safeClose(s.Close)

_, err = io.Copy(f, s)
return err
}

func safeClose(fn func() error) {
err := fn()
if err != nil {
log.Println(err)
}
}
20 changes: 20 additions & 0 deletions config/path_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// +build windows

package config

import (
"os/user"
"path/filepath"
)

const defaultFileName = ".prm"

// GetPath get the configuration file path.
func GetPath() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}

return filepath.Join(usr.HomeDir, defaultFileName), nil
}
2 changes: 1 addition & 1 deletion readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Feature:
* Remove (clean) all PR for a project.
* Push force a PR (auto).
* Manage all your repositories.
* Save your configuration: `~/.prm`
* Save your configuration: `config/prm` (or `~/.prm` on Windows)
* Only works with GitHub.
== How to Install
Expand Down

0 comments on commit dca5948

Please sign in to comment.