Use OS related default setup directory (#741)

* Use OS related default setup directory

* added missing comment

* adjust test-captplanet.sh

* ApplicationDir moved to fpath
This commit is contained in:
Michal Niewrzal 2018-12-03 16:51:56 +01:00 committed by GitHub
parent 0018ebf63e
commit 1206ef9126
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 49 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/spf13/cobra"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/internal/fpath"
"storj.io/storj/internal/memory"
"storj.io/storj/pkg/process"
)
@ -28,17 +29,20 @@ var (
Short: "Captain Planet! With our powers combined!",
}
defaultConfDir = "$HOME/.storj/capt"
defaultConfDir string
)
func main() {
go dumpHandler()
// process.Exec will load this for this command.
process.Exec(rootCmd)
}
func init() {
defaultConfDir = fpath.ApplicationDir("storj", "capt")
runCmd.Flags().String("config",
filepath.Join(defaultConfDir, "config.yaml"), "path to configuration")
setupCmd.Flags().String("config",
filepath.Join(defaultConfDir, "setup.yaml"), "path to configuration")
process.Exec(rootCmd)
}
// dumpHandler listens for Ctrl+\ on Unix

View File

@ -16,6 +16,7 @@ import (
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"storj.io/storj/internal/fpath"
"storj.io/storj/pkg/auth/grpcauth"
"storj.io/storj/pkg/bwagreement"
dbmanager "storj.io/storj/pkg/bwagreement/database-manager"
@ -86,10 +87,11 @@ var (
QListLimit int `help:"maximum segments that can be requested" default:"1000"`
}
defaultConfDir = "$HOME/.storj/satellite"
defaultConfDir string
)
func init() {
defaultConfDir = fpath.ApplicationDir("storj", "satellite")
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(setupCmd)
rootCmd.AddCommand(diagCmd)

View File

@ -14,6 +14,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/spf13/cobra"
"storj.io/storj/internal/fpath"
"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/kademlia"
"storj.io/storj/pkg/pb"
@ -59,11 +60,12 @@ var (
BasePath string `default:"$CONFDIR" help:"base path for setup"`
}
defaultConfDir = "$HOME/.storj/storagenode"
defaultConfDir string
defaultDiagDir = "$HOME/.storj/capt/f37/data"
)
func init() {
defaultConfDir = fpath.ApplicationDir("storj", "storagenode")
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(setupCmd)
rootCmd.AddCommand(diagCmd)

View File

@ -6,10 +6,7 @@ package cmd
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/spf13/cobra"
@ -39,46 +36,10 @@ var GWCmd = &cobra.Command{
Short: "The Storj client-side S3 gateway",
}
func applicationDir(subdir ...string) string {
for i := range subdir {
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
subdir[i] = strings.Title(subdir[i])
} else {
subdir[i] = strings.ToLower(subdir[i])
}
}
var appdir string
home := os.Getenv("HOME")
switch runtime.GOOS {
case "windows":
// Windows standards: https://msdn.microsoft.com/en-us/library/windows/apps/hh465094.aspx?f=255&MSPPError=-2147217396
for _, env := range []string{"AppData", "AppDataLocal", "UserProfile", "Home"} {
val := os.Getenv(env)
if val != "" {
appdir = val
break
}
}
case "darwin":
// Mac standards: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html
appdir = filepath.Join(home, "Library", "Application Support")
case "linux":
fallthrough
default:
// Linux standards: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
appdir = os.Getenv("XDG_DATA_HOME")
if appdir == "" && home != "" {
appdir = filepath.Join(home, ".local", "share")
}
}
return filepath.Join(append([]string{appdir}, subdir...)...)
}
func addCmd(cmd *cobra.Command, root *cobra.Command) *cobra.Command {
root.AddCommand(cmd)
defaultConfDir := applicationDir("storj", "uplink")
defaultConfDir := fpath.ApplicationDir("storj", "uplink")
cfgstruct.Bind(cmd.Flags(), &cfg, cfgstruct.ConfDir(defaultConfDir))
cmd.Flags().String("config", filepath.Join(defaultConfDir, "config.yaml"), "path to configuration")
return cmd

View File

@ -12,6 +12,7 @@ import (
base58 "github.com/jbenet/go-base58"
"github.com/spf13/cobra"
"storj.io/storj/internal/fpath"
"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/process"
"storj.io/storj/pkg/provider"
@ -36,7 +37,7 @@ var (
)
func init() {
defaultConfDir := applicationDir("storj", "uplink")
defaultConfDir := fpath.ApplicationDir("storj", "uplink")
CLICmd.AddCommand(setupCmd)
GWCmd.AddCommand(setupCmd)
cfgstruct.Bind(setupCmd.Flags(), &setupCfg, cfgstruct.ConfDir(defaultConfDir))
@ -62,7 +63,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) {
return err
}
defaultConfDir := applicationDir("storj", "uplink")
defaultConfDir := fpath.ApplicationDir("storj", "uplink")
// TODO: handle setting base path *and* identity file paths via args
// NB: if base path is set this overrides identity and CA path options
if setupCfg.BasePath != defaultConfDir {

View File

@ -7,8 +7,10 @@ import (
"errors"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
@ -120,3 +122,40 @@ func (p FPath) IsLocal() bool {
func (p FPath) String() string {
return p.original
}
// ApplicationDir returns best base directory for specific OS
func ApplicationDir(subdir ...string) string {
for i := range subdir {
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
subdir[i] = strings.Title(subdir[i])
} else {
subdir[i] = strings.ToLower(subdir[i])
}
}
var appdir string
home := os.Getenv("HOME")
switch runtime.GOOS {
case "windows":
// Windows standards: https://msdn.microsoft.com/en-us/library/windows/apps/hh465094.aspx?f=255&MSPPError=-2147217396
for _, env := range []string{"AppData", "AppDataLocal", "UserProfile", "Home"} {
val := os.Getenv(env)
if val != "" {
appdir = val
break
}
}
case "darwin":
// Mac standards: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html
appdir = filepath.Join(home, "Library", "Application Support")
case "linux":
fallthrough
default:
// Linux standards: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
appdir = os.Getenv("XDG_DATA_HOME")
if appdir == "" && home != "" {
appdir = filepath.Join(home, ".local", "share")
}
}
return filepath.Join(append([]string{appdir}, subdir...)...)
}

View File

@ -3,7 +3,7 @@ set -ueo pipefail
go install -v storj.io/storj/cmd/captplanet
captplanet setup --overwrite
sed -i~ 's/interval:.*/interval: 1s/g' $HOME/.storj/capt/config.yaml
sed -i~ 's/interval:.*/interval: 1s/g' $HOME/.local/share/storj/capt/config.yaml
# run captplanet for 5 seconds to reproduce kademlia problems. See V3-526
captplanet run &
@ -76,7 +76,7 @@ fi
kill -9 $CAPT_PID
captplanet setup --listen-host ::1 --overwrite
sed -i~ 's/interval:.*/interval: 1s/g' $HOME/.storj/capt/config.yaml
sed -i~ 's/interval:.*/interval: 1s/g' $HOME/.local/share/storj/capt/config.yaml
captplanet run &
CAPT_PID=$!