Use standards-based user paths in sensuctl
@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
+ "runtime"
homedir "github.com/mitchellh/go-homedir"
"github.com/sensu/sensu-go/cli"
@@ -16,13 +17,57 @@ import (
const OrganizationFlagDefault = "default"
var (
- // sensuPath contains the path to CLI configuration files
- sensuPath string
+ // sensuConfigPath contains the path to sensuctl configuration files
+ sensuConfigPath string
+
+ // sensuCachePath contains the path to sensuctl cache files
+ sensuCachePath string
)
-func init() {
+func userConfigPath() string {
+ switch runtime.GOOS {
+ case "windows":
+ appDataPath := os.Getenv("APPDATA")
+ if appDataPath == "" {
+ h, _ := homedir.Dir()
+ appDataPath = filepath.Join(h, "AppData", "Roaming")
+ }
+ return filepath.Join(appDataPath, "sensu", "sensuctl")
+ default:
+ xdgConfigPath := os.Getenv("XDG_CONFIG_HOME")
+ if xdgConfigPath == "" {
+ h, _ := homedir.Dir()
+ xdgConfigPath = filepath.Join(h, ".config")
+ }
+ return filepath.Join(xdgConfigPath, "sensu", "sensuctl")
+ }
+}
+
+func userCachePath() string {
+ switch runtime.GOOS {
+ case "windows":
+ localAppDataPath := os.Getenv("LOCALAPP")
+ if localAppDataPath == "" {
h, _ := homedir.Dir()
- sensuPath = filepath.Join(h, ".config", "sensu")
+ localAppDataPath = filepath.Join(h, "AppData", "Local")
+ }
+ return filepath.Join(localAppDataPath, "sensu", "sensuctl")
+ case "darwin":
+ h, _ := homedir.Dir()
+ return filepath.Join(h, "Library", "Caches", "sensu", "sensuctl")
+ default:
+ xdgCachePath := os.Getenv("XDG_CACHE_HOME")
+ if xdgCachePath == "" {
+ h, _ := homedir.Dir()
+ xdgCachePath = filepath.Join(h, ".cache")
+ }
+ return filepath.Join(xdgCachePath, "sensu", "sensuctl")
+ }
+}
+
+func init() {
+ sensuConfigPath = userConfigPath()
+ sensuCachePath = userCachePath()
}
func main() {
@@ -67,7 +112,8 @@ func configureRootCmd() *cobra.Command {
// Global flags
cmd.PersistentFlags().StringP("api-url", "", "", "host URL of Sensu installation")
- cmd.PersistentFlags().StringP("config-dir", "d", sensuPath, "directory of configuration files to load")
+ cmd.PersistentFlags().StringP("config-dir", "d", sensuConfigPath, "path to directory containing configuration files")
+ cmd.PersistentFlags().StringP("cache-dir", "t", sensuCachePath, "path to directory containing cache & temporary files")
cmd.PersistentFlags().StringP("organization", "", OrganizationFlagDefault, "organization in which we perform actions")
return cmd
