diff --git a/README.md b/README.md index 8733fc0..2e352b5 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ gup:INFO : If you want to update binaries, the following command. $ gup update mimixbox ``` ### Export/Import subcommand -You use the export/import subcommand if you want to install the same golang binaries across multiple systems. By default, export-subcommand exports the file to $HOME/.config/gup/gup.conf. After you have placed gup.conf in the same path hierarchy on another system, you execute import-subcommand. gup start the installation +You use the export/import subcommand if you want to install the same golang binaries across multiple systems. By default, export-subcommand exports the file to $XDG_CONFIG_HOME/gup/gup.conf. If you want to know [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html), see this link. After you have placed gup.conf in the same path hierarchy on another system, you execute import-subcommand. gup start the installation according to the contents of gup.conf. ``` diff --git a/cmd/export.go b/cmd/export.go index fdb54fa..7105b2c 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -17,10 +17,10 @@ var exportCmd = &cobra.Command{ Use the export subcommand if you want to install the same golang binaries across multiple systems. By default, this sub-command -exports the file to $HOME/.config/gup/gup.conf. After you have -placed gup.conf in the same path hierarchy on another system, -you execute import subcommand. gup start the installation -according to the contents of gup.conf.`, +exports the file to $XDG_CONFIG_HOME/.config/gup/gup.conf (e.g. $HOME/.config/gup/gup.conf.) +After you have placed gup.conf in the same path hierarchy on +another system, you execute import subcommand. gup start the +installation according to the contents of gup.conf.`, Run: func(cmd *cobra.Command, args []string) { OsExit(export(cmd, args)) }, diff --git a/cmd/export_test.go b/cmd/export_test.go index efe77f7..0645ba8 100644 --- a/cmd/export_test.go +++ b/cmd/export_test.go @@ -10,6 +10,7 @@ import ( "strings" "testing" + "github.com/adrg/xdg" "github.com/google/go-cmp/cmp" "github.com/nao1215/gup/internal/config" "github.com/nao1215/gup/internal/file" @@ -179,14 +180,10 @@ func Test_export(t *testing.T) { t.Setenv("GOBIN", tt.gobin) if tt.name == "can not make .config directory" { - oldHome := os.Getenv("HOME") - if err := os.Setenv("HOME", filepath.Join("/", "root")); err != nil { - t.Fatal(err) - } + oldHome := xdg.ConfigHome + xdg.ConfigHome = filepath.Join("/", "root") defer func() { - if err := os.Setenv("HOME", oldHome); err != nil { - t.Fatal(err) - } + xdg.ConfigHome = oldHome }() } diff --git a/cmd/import.go b/cmd/import.go index 30f46c3..99ab7e9 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -17,7 +17,7 @@ var importCmd = &cobra.Command{ Use the export subcommand if you want to install the same golang binaries across multiple systems. After you create gup.conf by import subcommand in another environment, you save conf-file in -$HOME/.config/gup/gup.conf. +$XDG_CONFIG_HOME/.config/gup/gup.conf (e.g. $HOME/.config/gup/gup.conf.) Finally, you execute the export subcommand in this state.`, Run: func(cmd *cobra.Command, args []string) { OsExit(runImport(cmd, args)) diff --git a/go.mod b/go.mod index ba92514..4691bed 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/nao1215/gup go 1.18 require ( + github.com/adrg/xdg v0.4.0 github.com/fatih/color v1.14.1 github.com/gen2brain/beeep v0.0.0-20220909211152-5a9ec94374f6 github.com/google/go-cmp v0.5.9 @@ -12,6 +13,7 @@ require ( ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 // indirect github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect diff --git a/go.sum b/go.sum index a129197..0252748 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,9 @@ +github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls= +github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w= github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg= github.com/gen2brain/beeep v0.0.0-20220909211152-5a9ec94374f6 h1:jFEK/SA/7E8lg9T33+y8D4Z0I782+bbiEjmyyklRzRQ= @@ -20,16 +25,24 @@ github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/ github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk= github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o= +golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/config/config.go b/internal/config/config.go index c465a7c..16dd80a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -9,6 +9,7 @@ import ( "regexp" "strings" + "github.com/adrg/xdg" "github.com/nao1215/gup/internal/cmdinfo" "github.com/nao1215/gup/internal/file" "github.com/nao1215/gup/internal/goutil" @@ -23,16 +24,9 @@ func FilePath() string { } // DirPath return directory path that store configuration-file. +// Default path is $HOME/.config/gup. func DirPath() string { - home, err := os.UserHomeDir() - if err != nil { - // If $HOME is empty, .config directory can be created in the - // current directory. The .config directory path is displayed - // when reporting the completion of the export subcommand. - // So, user notices that the output destination is strange. - return filepath.Join(os.Getenv("HOME"), ".config", cmdinfo.Name) - } - return filepath.Join(home, ".config", cmdinfo.Name) + return filepath.Join(xdg.ConfigHome, cmdinfo.Name) } // ReadConfFile return contents of configuration-file (package information)