Documentation
¶
Index ¶
- type Config
- func (c Config) Create(fileName string) (*os.File, error)
- func (c Config) CreateParentDir(fileName string) error
- func (c Config) Exists(fileName string) bool
- func (c Config) MkdirAll() error
- func (c Config) Open(fileName string) (*os.File, error)
- func (c Config) ReadFile(fileName string) ([]byte, error)
- func (c Config) WriteFile(fileName string, data []byte) error
- type ConfigDir
- type ConfigType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Path string
Type ConfigType
}
Config represents each folder
func (Config) CreateParentDir ¶
CreateParentDir creates the parent directory of fileName inside c. fileName is a relative path inside c, containing zero or more path separators.
type ConfigDir ¶
ConfigDir keeps setting for querying folders.
Example ¶
Sample for reading configuration
package main
import (
"encoding/json"
"github.com/shibukawa/configdir"
"path/filepath"
)
type Config struct {
UserName string `json:"user-name"`
}
var DefaultConfig = Config{
UserName: "baron",
}
func main() {
var config Config
configDirs := configdir.New("vendor-name", "application-name")
// optional: local path has the highest priority
configDirs.LocalPath, _ = filepath.Abs(".")
folder := configDirs.QueryFolderContainsFile("setting.json")
if folder != nil {
data, _ := folder.ReadFile("setting.json")
json.Unmarshal(data, &config)
} else {
config = DefaultConfig
}
}
Output:
func (ConfigDir) QueryCacheFolder ¶
Example ¶
Sample for getting cache folder
package main
import (
"github.com/shibukawa/configdir"
"io/ioutil"
"log"
"net/http"
)
func main() {
configDirs := configdir.New("vendor-name", "application-name")
cache := configDirs.QueryCacheFolder()
resp, err := http.Get("http://examples.com/sdk.zip")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
cache.WriteFile("sdk.zip", body)
}
Output:
func (ConfigDir) QueryFolderContainsFile ¶
func (ConfigDir) QueryFolders ¶
func (c ConfigDir) QueryFolders(configType ConfigType) []*Config
Example ¶
Sample for reading configuration
package main
import (
"encoding/json"
"github.com/shibukawa/configdir"
)
type Config struct {
UserName string `json:"user-name"`
}
func main() {
configDirs := configdir.New("vendor-name", "application-name")
var config Config
data, _ := json.Marshal(&config)
// Stores to local folder
folders := configDirs.QueryFolders(configdir.Local)
folders[0].WriteFile("setting.json", data)
// Stores to user folder
folders = configDirs.QueryFolders(configdir.Global)
folders[0].WriteFile("setting.json", data)
// Stores to system folder
folders = configDirs.QueryFolders(configdir.System)
folders[0].WriteFile("setting.json", data)
}
Output:
type ConfigType ¶
type ConfigType int
const ( System ConfigType = iota Global All Existing Local Cache )
Click to show internal directories.
Click to hide internal directories.