// Copyright 2021 storyicon@foxmail.com // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package consts import ( "os" "path/filepath" "github.com/storyicon/powerproto/pkg/util/logger" ) // defines a set of const value const ( // ConfigFileName defines the config file name ConfigFileName = "powerproto.yaml" // The default include can be referenced by this key in import paths KeyPowerProtoInclude = "$POWERPROTO_INCLUDE" // Defines the program directory of PowerProto, including various binary and include files EnvHomeDir = "POWERPROTO_HOME" // ProtobufRepository defines the protobuf repository ProtobufRepository = "https://github.com/protocolbuffers/protobuf" ) var homeDir string var log = logger.NewDefault("consts") // GetHomeDir is used to get cached homeDir func GetHomeDir() string { return homeDir } // PathForGlobalConfig is used to get path of global config func PathForGlobalConfig() string { return filepath.Join(GetHomeDir(), ConfigFileName) } func getHomeDir() (string, error) { val := os.Getenv(EnvHomeDir) if val != "" { return filepath.Abs(val) } homeDir, err := os.UserHomeDir() if err != nil { return "", err } return filepath.Join(homeDir, ".powerproto"), nil } func init() { var err error homeDir, err = getHomeDir() if err != nil { log.LogFatal(nil, "Please set the working directory of PowerProto by "+ "configuring the environment variable %s", EnvHomeDir) } }