本文整理匯總了Golang中github.com/minio/minio/pkg/quick.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestDeepDiff
func (s *MySuite) TestDeepDiff(c *C) {
type myStruct struct {
Version string
User string
Password string
Folders []string
}
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
config, err := quick.New(&saveMe)
c.Assert(err, IsNil)
c.Assert(config, Not(IsNil))
mismatch := myStruct{"1", "Guest", "nopassword", []string{"Work", "documents", "Music"}}
newConfig, err := quick.New(&mismatch)
c.Assert(err, IsNil)
c.Assert(newConfig, Not(IsNil))
fields, err := config.DeepDiff(newConfig)
c.Assert(err, IsNil)
c.Assert(len(fields), Equals, 2)
// Uncomment for debugging
// for i, field := range fields {
// fmt.Printf("DeepDiff[%d]: %s=%v\n", i, field.Name(), field.Value())
// }
}
示例2: removeAlias
// removeAlias - remove alias
func removeAlias(alias string) {
if alias == "" {
fatalIf(errDummy().Trace(), "Alias or URL cannot be empty.")
}
conf := newConfigV2()
config, err := quick.New(conf)
fatalIf(err.Trace(conf.Version), "Failed to initialize ‘quick’ configuration data structure.")
err = config.Load(mustGetMcConfigPath())
fatalIf(err.Trace(), "Unable to load config path")
if isAliasReserved(alias) {
fatalIf(errDummy().Trace(), fmt.Sprintf("Cannot use a reserved name ‘%s’ as an alias. Following are reserved names: [help, private, readonly, public, authenticated].", alias))
}
if !isValidAliasName(alias) {
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias name ‘%s’ is invalid, valid examples are: mybucket, Area51, Grand-Nagus", alias))
}
// convert interface{} back to its original struct
newConf := config.Data().(*configV2)
if _, ok := newConf.Aliases[alias]; !ok {
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias ‘%s’ does not exist.", alias))
}
delete(newConf.Aliases, alias)
newConfig, err := quick.New(newConf)
fatalIf(err.Trace(conf.Version), "Failed to initialize ‘quick’ configuration data structure.")
err = writeConfig(newConfig)
fatalIf(err.Trace(alias), "Unable to save alias ‘"+alias+"’.")
console.Println(AliasMessage{
op: "remove",
Alias: alias,
})
}
示例3: TestSaveLoad
func (s *MySuite) TestSaveLoad(c *C) {
defer os.RemoveAll("test.json")
type myStruct struct {
Version string
User string
Password string
Folders []string
}
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
config, err := quick.New(&saveMe)
c.Assert(err, IsNil)
c.Assert(config, Not(IsNil))
err = config.Save("test.json")
c.Assert(err, IsNil)
loadMe := myStruct{Version: "1"}
newConfig, err := quick.New(&loadMe)
c.Assert(err, IsNil)
c.Assert(newConfig, Not(IsNil))
err = newConfig.Load("test.json")
c.Assert(err, IsNil)
c.Assert(config.Data(), DeepEquals, newConfig.Data())
c.Assert(config.Data(), DeepEquals, &loadMe)
mismatch := myStruct{"1.1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
c.Assert(newConfig.Data(), Not(DeepEquals), &mismatch)
}
示例4: removeAlias
// removeAlias - remove alias
func removeAlias(alias string) {
if strings.TrimSpace(alias) == "" {
fatalIf(errDummy().Trace(), "Alias or URL cannot be empty.")
}
config, err := newConfig()
fatalIf(err.Trace(), "Failed to initialize ‘quick’ configuration data structure.")
configPath := mustGetMcConfigPath()
err = config.Load(configPath)
fatalIf(err.Trace(configPath), "Unable to load config path")
if !isValidAliasName(alias) {
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias name ‘%s’ is invalid, valid examples are: mybucket, Area51, Grand-Nagus", alias))
}
// convert interface{} back to its original struct
newConf := config.Data().(*configV5)
if _, ok := newConf.Aliases[alias]; !ok {
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias ‘%s’ does not exist.", alias))
}
delete(newConf.Aliases, alias)
newConfig, err := quick.New(newConf)
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
err = writeConfig(newConfig)
fatalIf(err.Trace(alias), "Unable to save alias ‘"+alias+"’.")
Prints("%s\n", AliasMessage{
op: "remove",
Alias: alias,
})
}
示例5: migrateConfigV4ToV5
// Migrate config version ‘4’ to ‘5’. Rename hostConfigV4.Signature -> hostConfigV5.API.
func migrateConfigV4ToV5() {
if !isMcConfigExists() {
return
}
mcCfgV4, e := quick.Load(mustGetMcConfigPath(), newConfigV4())
fatalIf(probe.NewError(e), "Unable to load mc config V4.")
// update to newer version
if mcCfgV4.Version() != "4" {
return
}
cfgV5 := newConfigV5()
for k, v := range mcCfgV4.Data().(*configV4).Aliases {
cfgV5.Aliases[k] = v
}
for host, hostCfgV4 := range mcCfgV4.Data().(*configV4).Hosts {
cfgV5.Hosts[host] = hostConfigV5{
AccessKeyID: hostCfgV4.AccessKeyID,
SecretAccessKey: hostCfgV4.SecretAccessKey,
API: "v4", // Rename from .Signature to .API
}
}
mcNewCfgV5, e := quick.New(cfgV5)
fatalIf(probe.NewError(e), "Unable to initialize quick config for config version ‘5’.")
e = mcNewCfgV5.Save(mustGetMcConfigPath())
fatalIf(probe.NewError(e), "Unable to save config version ‘5’.")
console.Infof("Successfully migrated %s from version ‘4’ to version ‘5’.\n", mustGetMcConfigPath())
}
示例6: migrateConfigV101ToV2
// Migrate from config ‘1.0.1’ to ‘2’. Drop semantic versioning and move to integer versioning. No other changes.
func migrateConfigV101ToV2() {
if !isMcConfigExists() {
return
}
mcCfgV101, e := quick.Load(mustGetMcConfigPath(), newConfigV101())
fatalIf(probe.NewError(e), "Unable to load config version ‘1.0.1’.")
// update to newer version
if mcCfgV101.Version() != "1.0.1" {
return
}
cfgV2 := newConfigV2()
// Copy aliases.
for k, v := range mcCfgV101.Data().(*configV101).Aliases {
cfgV2.Aliases[k] = v
}
// Copy hosts.
for k, hostCfgV101 := range mcCfgV101.Data().(*configV101).Hosts {
cfgV2.Hosts[k] = hostConfigV2{
AccessKeyID: hostCfgV101.AccessKeyID,
SecretAccessKey: hostCfgV101.SecretAccessKey,
}
}
mcCfgV2, e := quick.New(cfgV2)
fatalIf(probe.NewError(e), "Unable to initialize quick config for config version ‘2’.")
e = mcCfgV2.Save(mustGetMcConfigPath())
fatalIf(probe.NewError(e), "Unable to save config version ‘2’.")
console.Infof("Successfully migrated %s from version ‘1.0.1’ to version ‘2’.\n", mustGetMcConfigPath())
}
示例7: loadSessionV6Header
func loadSessionV6Header(sid string) (*sessionV6Header, *probe.Error) {
if !isSessionDirExists() {
return nil, errInvalidArgument().Trace()
}
sessionFile, err := getSessionFile(sid)
if err != nil {
return nil, err.Trace(sid)
}
if _, e := os.Stat(sessionFile); e != nil {
return nil, probe.NewError(e)
}
sV6Header := &sessionV6Header{}
sV6Header.Version = "6"
qs, e := quick.New(sV6Header)
if e != nil {
return nil, probe.NewError(e).Trace(sid, sV6Header.Version)
}
e = qs.Load(sessionFile)
if e != nil {
return nil, probe.NewError(e).Trace(sid, sV6Header.Version)
}
sV6Header = qs.Data().(*sessionV6Header)
return sV6Header, nil
}
示例8: removeHost
func removeHost(hostGlob string) {
if strings.TrimSpace(hostGlob) == "" {
fatalIf(errDummy().Trace(), "Alias or URL cannot be empty.")
}
if strings.TrimSpace(hostGlob) == "dl.minio.io:9000" {
fatalIf(errDummy().Trace(), "‘"+hostGlob+"’ is reserved hostname and cannot be removed.")
}
config, err := newConfig()
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
configPath := mustGetMcConfigPath()
err = config.Load(configPath)
fatalIf(err.Trace(configPath), "Unable to load config path")
// convert interface{} back to its original struct
newConf := config.Data().(*configV5)
if _, ok := newConf.Hosts[hostGlob]; !ok {
fatalIf(errDummy().Trace(), fmt.Sprintf("Host glob ‘%s’ does not exist.", hostGlob))
}
delete(newConf.Hosts, hostGlob)
newConfig, err := quick.New(newConf)
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
err = writeConfig(newConfig)
fatalIf(err.Trace(hostGlob), "Unable to save host glob ‘"+hostGlob+"’.")
Prints("%s\n", HostMessage{
op: "remove",
Host: hostGlob,
})
}
示例9: Load
// Load shareDB entries from disk. Any entries held in memory are reset.
func (s *shareDBV1) Load(filename string) *probe.Error {
s.mutex.Lock()
defer s.mutex.Unlock()
// Check if the db file exist.
if _, e := os.Stat(filename); e != nil {
return probe.NewError(e)
}
// Initialize and load using quick package.
qs, e := quick.New(newShareDBV1())
if e != nil {
return probe.NewError(e).Trace(filename)
}
e = qs.Load(filename)
if e != nil {
return probe.NewError(e).Trace(filename)
}
// Copy map over.
for k, v := range qs.Data().(*shareDBV1).Shares {
s.Shares[k] = v
}
// Filter out expired entries and save changes back to disk.
s.deleteAllExpired()
s.save(filename)
return nil
}
示例10: addAlias
// addAlias - add new aliases
func addAlias(alias, url string) {
if alias == "" || url == "" {
fatalIf(errDummy().Trace(), "Alias or URL cannot be empty.")
}
config, err := newConfig()
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
err = config.Load(mustGetMcConfigPath())
fatalIf(err.Trace(), "Unable to load config path")
url = strings.TrimSuffix(url, "/")
if !strings.HasPrefix(url, "http") {
fatalIf(errDummy().Trace(), fmt.Sprintf("Invalid alias URL ‘%s’. Valid examples are: http://s3.amazonaws.com, https://yourbucket.example.com.", url))
}
if !isValidAliasName(alias) {
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias name ‘%s’ is invalid, valid examples are: mybucket, Area51, Grand-Nagus", alias))
}
// convert interface{} back to its original struct
newConf := config.Data().(*configV5)
if oldURL, ok := newConf.Aliases[alias]; ok {
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias ‘%s’ already exists for ‘%s’.", alias, oldURL))
}
newConf.Aliases[alias] = url
newConfig, err := quick.New(newConf)
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
err = writeConfig(newConfig)
fatalIf(err.Trace(alias, url), "Unable to save alias ‘"+alias+"’.")
Prints("%s\n", AliasMessage{
op: "add",
Alias: alias,
URL: url,
})
}
示例11: getMcConfig
// getMcConfig - reads configuration file and returns config
func getMcConfig() (*configV2, *probe.Error) {
if !isMcConfigExists() {
return nil, errInvalidArgument().Trace()
}
configFile, err := getMcConfigPath()
if err != nil {
return nil, err.Trace()
}
// Cached in private global variable.
if v := cache.Get(); v != nil { // Use previously cached config.
return v.(quick.Config).Data().(*configV2), nil
}
conf := newConfigV2()
qconf, err := quick.New(conf)
if err != nil {
return nil, err.Trace()
}
err = qconf.Load(configFile)
if err != nil {
return nil, err.Trace()
}
cache.Put(qconf)
return qconf.Data().(*configV2), nil
}
示例12: migrateV3ToV4
// Version '3' to '4' migrates config, removes previous fields related
// to backend types and server address. This change further simplifies
// the config for future additions.
func migrateV3ToV4() {
cv3, err := loadConfigV3()
if err != nil && os.IsNotExist(err) {
return
}
fatalIf(err, "Unable to load config version ‘3’.")
if cv3.Version != "3" {
return
}
// Save only the new fields, ignore the rest.
srvConfig := &configV4{}
srvConfig.Version = "4"
srvConfig.Credential = cv3.Credential
srvConfig.Region = cv3.Region
if srvConfig.Region == "" {
// Region needs to be set for AWS Signature Version 4.
srvConfig.Region = "us-east-1"
}
srvConfig.Logger.Console = cv3.Logger.Console
srvConfig.Logger.File = cv3.Logger.File
srvConfig.Logger.Syslog = cv3.Logger.Syslog
qc, err := quick.New(srvConfig)
fatalIf(err, "Unable to initialize the quick config.")
configFile, err := getConfigFile()
fatalIf(err, "Unable to get config file.")
err = qc.Save(configFile)
fatalIf(err, "Failed to migrate config from ‘"+cv3.Version+"’ to ‘"+srvConfig.Version+"’ failed.")
console.Println("Migration from version ‘" + cv3.Version + "’ to ‘" + srvConfig.Version + "’ completed successfully.")
}
示例13: loadSessionV1
// loadSession - reads session file if exists and re-initiates internal variables
func loadSessionV1(sid string) (*sessionV1, *probe.Error) {
if !isSessionDirExists() {
return nil, probe.NewError(errors.New("Session folder does not exist."))
}
sessionFile, err := getSessionFileV1(sid)
if err != nil {
return nil, err.Trace(sid)
}
s := new(sessionV1)
s.Version = "1.0.0"
// map of command and files copied
s.URLs = nil
s.Lock = new(sync.Mutex)
s.Files = make(map[string]bool)
qs, err := quick.New(s)
if err != nil {
return nil, err.Trace(s.Version)
}
err = qs.Load(sessionFile)
if err != nil {
return nil, err.Trace(sessionFile, s.Version)
}
return qs.Data().(*sessionV1), nil
}
示例14: isModified
// IsModified - returns if in memory session header has changed from
// its on disk value.
func (s *sessionV8) isModified(sessionFile string) (bool, *probe.Error) {
qs, e := quick.New(s.Header)
if e != nil {
return false, probe.NewError(e).Trace(s.SessionID)
}
var currentHeader = &sessionV8Header{}
currentQS, e := quick.Load(sessionFile, currentHeader)
if e != nil {
// If session does not exist for the first, return modified to
// be true.
if os.IsNotExist(e) {
return true, nil
}
// For all other errors return.
return false, probe.NewError(e).Trace(s.SessionID)
}
changedFields, e := qs.DeepDiff(currentQS)
if e != nil {
return false, probe.NewError(e).Trace(s.SessionID)
}
// Returns true if there are changed entries.
return len(changedFields) > 0, nil
}
示例15: TestVersion
func (s *MySuite) TestVersion(c *C) {
defer os.RemoveAll("test.json")
type myStruct struct {
Version string
User string
Password string
Folders []string
}
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
config, err := quick.New(&saveMe)
c.Assert(err, IsNil)
c.Assert(config, Not(IsNil))
err = config.Save("test.json")
c.Assert(err, IsNil)
valid, err := quick.CheckVersion("test.json", "1")
c.Assert(err, IsNil)
c.Assert(valid, Equals, true)
valid, err = quick.CheckVersion("test.json", "2")
c.Assert(err, IsNil)
c.Assert(valid, Equals, false)
_, err = quick.CheckVersion("test1.json", "1")
c.Assert(err, Not(IsNil))
file, err := os.Create("test.json")
c.Assert(err, IsNil)
c.Assert(file.Close(), IsNil)
_, err = quick.CheckVersion("test.json", "1")
c.Assert(err, Not(IsNil))
}