Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert relative --data-dir paths to absolute. #10

Merged
merged 1 commit into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/atlantisnorth/atlantis/server"
Expand Down Expand Up @@ -293,15 +294,25 @@ func (s *ServerCmd) setAtlantisURL(config *server.Config) error {

// setDataDir checks if ~ was used in data-dir and converts it to the actual
// home directory. If we don't do this, we'll create a directory called "~"
// instead of actually using home.
// instead of actually using home. It also converts relative paths to absolute.
func (s *ServerCmd) setDataDir(config *server.Config) error {
if strings.HasPrefix(config.DataDir, "~/") {
expanded, err := homedir.Expand(config.DataDir)
finalPath := config.DataDir

// Convert ~ to the actual home dir.
if strings.HasPrefix(finalPath, "~/") {
var err error
finalPath, err = homedir.Expand(finalPath)
if err != nil {
return errors.Wrap(err, "determining home directory")
}
config.DataDir = expanded
}

// Convert relative paths to absolute.
finalPath, err := filepath.Abs(finalPath)
if err != nil {
return errors.Wrap(err, "making data-dir absolute")
}
config.DataDir = finalPath
return nil
}

Expand Down
30 changes: 24 additions & 6 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd_test
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -283,8 +284,8 @@ func TestExecute_Defaults(t *testing.T) {
Equals(t, 4141, passedConfig.Port)
}

func TestExecute_ExpandHomeDir(t *testing.T) {
t.Log("Should expand the ~ in the home dir to the actual home dir.")
func TestExecute_ExpandHomeInDataDir(t *testing.T) {
t.Log("If ~ is used as a data-dir path, should expand to absolute home path")
c := setup(map[string]interface{}{
cmd.GHUserFlag: "user",
cmd.GHTokenFlag: "token",
Expand All @@ -298,6 +299,23 @@ func TestExecute_ExpandHomeDir(t *testing.T) {
Equals(t, home+"/this/is/a/path", passedConfig.DataDir)
}

func TestExecute_RelativeDataDir(t *testing.T) {
t.Log("Should convert relative dir to absolute.")
c := setup(map[string]interface{}{
cmd.GHUserFlag: "user",
cmd.GHTokenFlag: "token",
cmd.DataDirFlag: "../",
})

// Figure out what ../ should be as an absolute path.
expectedAbsolutePath, err := filepath.Abs("../")
Ok(t, err)

err = c.Execute()
Ok(t, err)
Equals(t, expectedAbsolutePath, passedConfig.DataDir)
}

func TestExecute_GithubUser(t *testing.T) {
t.Log("Should remove the @ from the github username if it's passed.")
c := setup(map[string]interface{}{
Expand Down Expand Up @@ -326,7 +344,7 @@ func TestExecute_Flags(t *testing.T) {
t.Log("Should use all flags that are set.")
c := setup(map[string]interface{}{
cmd.AtlantisURLFlag: "url",
cmd.DataDirFlag: "path",
cmd.DataDirFlag: "/path",
cmd.GHHostnameFlag: "ghhostname",
cmd.GHUserFlag: "user",
cmd.GHTokenFlag: "token",
Expand All @@ -343,7 +361,7 @@ func TestExecute_Flags(t *testing.T) {
Ok(t, err)

Equals(t, "url", passedConfig.AtlantisURL)
Equals(t, "path", passedConfig.DataDir)
Equals(t, "/path", passedConfig.DataDir)
Equals(t, "ghhostname", passedConfig.GithubHostname)
Equals(t, "user", passedConfig.GithubUser)
Equals(t, "token", passedConfig.GithubToken)
Expand All @@ -361,7 +379,7 @@ func TestExecute_ConfigFile(t *testing.T) {
t.Log("Should use all the values from the config file.")
tmpFile := tempFile(t, `---
atlantis-url: "url"
data-dir: "path"
data-dir: "/path"
gh-hostname: "ghhostname"
gh-user: "user"
gh-token: "token"
Expand All @@ -381,7 +399,7 @@ require-approval: true`)
err := c.Execute()
Ok(t, err)
Equals(t, "url", passedConfig.AtlantisURL)
Equals(t, "path", passedConfig.DataDir)
Equals(t, "/path", passedConfig.DataDir)
Equals(t, "ghhostname", passedConfig.GithubHostname)
Equals(t, "user", passedConfig.GithubUser)
Equals(t, "token", passedConfig.GithubToken)
Expand Down