本文整理匯總了Golang中github.com/minio/mc/pkg/client.NewURL函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewURL函數的具體用法?Golang NewURL怎麽用?Golang NewURL使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewURL函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: doDiffMain
// doDiffMain runs the diff.
func doDiffMain(firstURL, secondURL string) {
// Source and targets are always directories
sourceSeparator := string(client.NewURL(firstURL).Separator)
if !strings.HasSuffix(firstURL, sourceSeparator) {
firstURL = firstURL + sourceSeparator
}
targetSeparator := string(client.NewURL(secondURL).Separator)
if !strings.HasSuffix(secondURL, targetSeparator) {
secondURL = secondURL + targetSeparator
}
// Expand aliased urls.
firstAlias, firstURL, _ := mustExpandAlias(firstURL)
secondAlias, secondURL, _ := mustExpandAlias(secondURL)
firstClient, err := newClientFromAlias(firstAlias, firstURL)
if err != nil {
fatalIf(err.Trace(firstAlias, firstURL, secondAlias, secondURL),
fmt.Sprintf("Failed to diff '%s' and '%s'", firstURL, secondURL))
}
difference, err := objectDifferenceFactory(secondAlias, secondURL)
if err != nil {
fatalIf(err.Trace(firstAlias, firstURL, secondAlias, secondURL),
fmt.Sprintf("Failed to diff '%s' and '%s'", firstURL, secondURL))
}
isRecursive := true
isIncomplete := false
for sourceContent := range firstClient.List(isRecursive, isIncomplete) {
if sourceContent.Err != nil {
switch sourceContent.Err.ToGoError().(type) {
// Handle this specifically for filesystem related errors.
case client.BrokenSymlink, client.TooManyLevelsSymlink, client.PathNotFound, client.PathInsufficientPermission:
errorIf(sourceContent.Err.Trace(firstURL, secondURL), fmt.Sprintf("Failed on '%s'", firstURL))
default:
fatalIf(sourceContent.Err.Trace(firstURL, secondURL), fmt.Sprintf("Failed on '%s'", firstURL))
}
continue
}
if sourceContent.Type.IsDir() {
continue
}
suffix := strings.TrimPrefix(sourceContent.URL.String(), firstURL)
differ, err := difference(suffix, sourceContent.Type, sourceContent.Size)
if err != nil {
errorIf(sourceContent.Err.Trace(secondURL, suffix),
fmt.Sprintf("Failed on '%s'", urlJoinPath(secondURL, suffix)))
continue
}
if differ == differNone {
continue
}
printMsg(diffMessage{
FirstURL: sourceContent.URL.String(),
SecondURL: urlJoinPath(secondURL, suffix),
Diff: differ,
})
}
}
示例2: urlJoinPath
// urlJoinPath Join a path to existing URL.
func urlJoinPath(url1, url2 string) string {
u1 := client.NewURL(url1)
u2 := client.NewURL(url2)
if u1.Path != string(u1.Separator) {
u1.Path = filepath.Join(u1.Path, u2.Path)
} else {
u1.Path = u2.Path
}
return u1.String()
}
示例3: prepareCopyURLsTypeC
// SINGLE SOURCE - Type C: copy(d1..., d2) -> []copy(d1/f, d1/d2/f) -> []A
// prepareCopyRecursiveURLTypeC - prepares target and source URLs for copying.
func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs {
copyURLsCh := make(chan copyURLs)
go func(sourceURL, targetURL string, copyURLsCh chan copyURLs) {
defer close(copyURLsCh)
if !isURLRecursive(sourceURL) {
// Source is not of recursive type.
copyURLsCh <- copyURLs{Error: errSourceNotRecursive(sourceURL).Trace()}
return
}
// add `/` after trimming off `...` to emulate folders
sourceURL = stripRecursiveURL(sourceURL)
sourceClient, sourceContent, err := url2Stat(sourceURL)
if err != nil {
// Source does not exist or insufficient privileges.
copyURLsCh <- copyURLs{Error: err.Trace(sourceURL)}
return
}
if !sourceContent.Type.IsDir() {
// Source is not a dir.
copyURLsCh <- copyURLs{Error: errSourceIsNotDir(sourceURL).Trace()}
return
}
for sourceContent := range sourceClient.List(true) {
if sourceContent.Err != nil {
// Listing failed.
copyURLsCh <- copyURLs{Error: sourceContent.Err.Trace()}
continue
}
if !sourceContent.Content.Type.IsRegular() {
// Source is not a regular file. Skip it for copy.
continue
}
// All OK.. We can proceed. Type B: source is a file, target is a folder and exists.
sourceURLParse := client.NewURL(sourceURL)
targetURLParse := client.NewURL(targetURL)
sourceURLDelimited := sourceURLParse.String()[:strings.LastIndex(sourceURLParse.String(),
string(sourceURLParse.Separator))+1]
sourceContentName := sourceContent.Content.Name
sourceContentURL := sourceURLDelimited + sourceContentName
sourceContentParse := client.NewURL(sourceContentURL)
// Construct target path from recursive path of source without its prefix dir.
newTargetURLParse := *targetURLParse
newTargetURLParse.Path = filepath.Join(newTargetURLParse.Path, sourceContentName)
copyURLsCh <- prepareCopyURLsTypeA(sourceContentParse.String(), newTargetURLParse.String())
}
}(sourceURL, targetURL, copyURLsCh)
return copyURLsCh
}
示例4: isObjectKeyPresent
// this code is necessary since, share only operates on cloud storage URLs not filesystem
func isObjectKeyPresent(url string) bool {
u := client.NewURL(url)
path := u.Path
matchS3, _ := filepath.Match("*.s3*.amazonaws.com", u.Host)
if matchS3 {
hostSplits := strings.SplitN(u.Host, ".", 2)
path = string(u.Separator) + hostSplits[0] + u.Path
}
matchGcs, _ := filepath.Match("*.storage.googleapis.com", u.Host)
if matchGcs {
hostSplits := strings.SplitN(u.Host, ".", 2)
path = string(u.Separator) + hostSplits[0] + u.Path
}
pathSplits := strings.SplitN(path, "?", 2)
splits := strings.SplitN(pathSplits[0], string(u.Separator), 3)
switch len(splits) {
case 0, 1:
return false
case 2:
return false
case 3:
if splits[2] == "" {
return false
}
return true
}
return false
}
示例5: checkRmSyntax
// Validate command line arguments.
func checkRmSyntax(ctx *cli.Context) {
args := ctx.Args()
ishelp := ctx.GlobalBool("help")
isForce := ctx.Bool("force")
if !args.Present() || ishelp {
exitCode := 1
cli.ShowCommandHelpAndExit(ctx, "rm", exitCode)
}
URLs, err := args2URLs(args)
fatalIf(err.Trace(ctx.Args()...), "Unable to parse arguments.")
// If input validation fails then provide context sensitive help without displaying generic help message.
// The context sensitive help is shown per argument instead of all arguments to keep the help display
// as well as the code simple. Also most of the times there will be just one arg
for _, url := range URLs {
u := client.NewURL(url)
if strings.HasSuffix(url, string(u.Separator)) {
fatalIf(errDummy().Trace(),
"‘"+url+"’ is a folder. To remove this folder recursively, please try ‘"+url+"...’ as argument.")
}
if isURLRecursive(url) && !isForce {
fatalIf(errDummy().Trace(),
"Recursive removal requires --force option. Please review carefully before performing this operation.")
}
}
}
示例6: rmAll
// Remove all objects recursively.
func rmAll(url string, isIncomplete bool) {
// Initialize new client.
clnt, err := url2Client(url)
if err != nil {
errorIf(err.Trace(url), "Invalid URL ‘"+url+"’.")
return // End of journey.
}
isRecursive := false // Disable recursion and only list this folder's contents.
for entry := range clnt.List(isRecursive, isIncomplete) {
if entry.Err != nil {
errorIf(entry.Err.Trace(url), "Unable to list ‘"+url+"’.")
return // End of journey.
}
if entry.Content.Type.IsDir() {
// Add separator at the end to remove all its contents.
url := entry.Content.URL.String()
u := client.NewURL(url)
url = url + string(u.Separator)
// Recursively remove contents of this directory.
rmAll(url, isIncomplete)
}
// Regular type.
rm(entry.Content.URL.String(), isIncomplete)
}
}
示例7: getHostConfig
// getHostConfig retrieves host specific configuration such as access keys, certs.
func getHostConfig(URL string) (hostConfig, *probe.Error) {
config, err := getMcConfig()
if err != nil {
return hostConfig{}, err.Trace()
}
url := client.NewURL(URL)
// No host matching or keys needed for filesystem requests
if url.Type == client.Filesystem {
hostCfg := hostConfig{
AccessKeyID: "",
SecretAccessKey: "",
API: "fs",
}
return hostCfg, nil
}
if _, ok := config.Hosts[url.Host]; ok {
return config.Hosts[url.Host], nil
}
for globURL, hostCfg := range config.Hosts {
match, err := filepath.Match(globURL, url.Host)
if err != nil {
return hostConfig{}, errInvalidGlobURL(globURL, URL).Trace()
}
if match {
return hostCfg, nil
}
}
return hostConfig{}, errNoMatchingHost(URL).Trace()
}
示例8: New
// New - instantiate a new fs client
func New(path string) (client.Client, *probe.Error) {
if strings.TrimSpace(path) == "" {
return nil, probe.NewError(client.EmptyPath{})
}
return &fsClient{
PathURL: client.NewURL(normalizePath(path)),
}, nil
}
示例9: guessURLContentType
// guessURLContentType - guess content-type of the URL.
// on failure just return 'application/octet-stream'.
func guessURLContentType(urlStr string) string {
url := client.NewURL(urlStr)
extension := strings.TrimPrefix(filepath.Ext(url.Path), ".")
contentType, e := contentdb.Lookup(extension)
if e != nil {
return "application/octet-stream"
}
return contentType
}
示例10: prepareCopyURLsTypeB
// SINGLE SOURCE - Type B: copy(f, d) -> copy(f, d/f) -> A
// prepareCopyURLsTypeB - prepares target and source URLs for copying.
func prepareCopyURLsTypeB(sourceURL string, targetURL string) copyURLs {
_, sourceContent, err := url2Stat(sourceURL)
if err != nil {
// Source does not exist or insufficient privileges.
return copyURLs{Error: err.Trace(sourceURL)}
}
if !sourceContent.Type.IsRegular() {
// Source is not a regular file.
return copyURLs{Error: errInvalidSource(sourceURL).Trace()}
}
// All OK.. We can proceed. Type B: source is a file, target is a folder and exists.
{
sourceURLParse := client.NewURL(sourceURL)
targetURLParse := client.NewURL(targetURL)
targetURLParse.Path = filepath.Join(targetURLParse.Path, filepath.Base(sourceURLParse.Path))
return prepareCopyURLsTypeA(sourceURL, targetURLParse.String())
}
}
示例11: checkRmSyntax
func checkRmSyntax(ctx *cli.Context) {
args := ctx.Args()
var force bool
var incomplete bool
if !args.Present() || args.First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "rm", 1) // last argument is exit code.
}
if len(args) == 1 && args.Get(0) == "force" {
return
}
if len(args) == 2 && args.Get(0) == "force" && args.Get(1) == "incomplete" ||
len(args) == 2 && args.Get(1) == "force" && args.Get(0) == "incomplete" {
return
}
if args.Last() == "force" {
force = true
args = args[:len(args)-1]
}
if args.Last() == "incomplete" {
incomplete = true
args = args[:len(args)-1]
}
// By this time we have sanitized the input args and now we have only the URLs parse them properly
// and validate.
URLs, err := args2URLs(args)
fatalIf(err.Trace(ctx.Args()...), "Unable to parse arguments.")
// If input validation fails then provide context sensitive help without displaying generic help message.
// The context sensitive help is shown per argument instead of all arguments to keep the help display
// as well as the code simple. Also most of the times there will be just one arg
for _, url := range URLs {
u := client.NewURL(url)
var helpStr string
if strings.HasSuffix(url, string(u.Separator)) {
if incomplete {
helpStr = "Usage : mc rm " + url + recursiveSeparator + " incomplete force"
} else {
helpStr = "Usage : mc rm " + url + recursiveSeparator + " force"
}
fatalIf(errDummy().Trace(), helpStr)
}
if isURLRecursive(url) && !force {
if incomplete {
helpStr = "Usage : mc rm " + url + " incomplete force"
} else {
helpStr = "Usage : mc rm " + url + " force"
}
fatalIf(errDummy().Trace(), helpStr)
}
}
}
示例12: prepareCopyURLsTypeC
// SINGLE SOURCE - Type C: copy(d1..., d2) -> []copy(d1/f, d1/d2/f) -> []A
// prepareCopyRecursiveURLTypeC - prepares target and source URLs for copying.
func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs {
copyURLsCh := make(chan copyURLs)
go func(sourceURL, targetURL string, copyURLsCh chan copyURLs) {
defer close(copyURLsCh)
if !isURLRecursive(sourceURL) {
// Source is not of recursive type.
copyURLsCh <- copyURLs{Error: errSourceNotRecursive(sourceURL).Trace()}
return
}
// add `/` after trimming off `...` to emulate folders
sourceURL = stripRecursiveURL(sourceURL)
sourceClient, sourceContent, err := url2Stat(sourceURL)
if err != nil {
// Source does not exist or insufficient privileges.
copyURLsCh <- copyURLs{Error: err.Trace(sourceURL)}
return
}
if !sourceContent.Type.IsDir() {
// Source is not a dir.
copyURLsCh <- copyURLs{Error: errSourceIsNotDir(sourceURL).Trace()}
return
}
for sourceContent := range sourceClient.List(true, false) {
if sourceContent.Err != nil {
// Listing failed.
copyURLsCh <- copyURLs{Error: sourceContent.Err.Trace()}
continue
}
if !sourceContent.Content.Type.IsRegular() {
// Source is not a regular file. Skip it for copy.
continue
}
// All OK.. We can proceed. Type B: source is a file, target is a folder and exists.
newTargetURL := client.NewURL(targetURL)
newTargetURL.Path = filepath.Join(newTargetURL.Path,
strings.TrimPrefix(sourceContent.Content.URL.Path, url2Dir(sourceURL)))
// verify if destination exists, and cpForceFlag is not set do not proceed.
_, _, err := url2Stat(newTargetURL.String())
if err == nil && !cpForceFlag {
copyURLsCh <- copyURLs{Error: errOverWriteNotAllowed(newTargetURL.String()).Trace()}
return
}
copyURLsCh <- prepareCopyURLsTypeA(sourceContent.Content.URL.String(), newTargetURL.String())
}
}(sourceURL, targetURL, copyURLsCh)
return copyURLsCh
}
示例13: url2Dir
// just like filepath.Dir but always has a trailing url.Seperator
func url2Dir(urlStr string) string {
url := client.NewURL(urlStr)
if strings.HasSuffix(urlStr, string(url.Separator)) {
return urlStr
}
lastIndex := strings.LastIndex(urlStr, string(url.Separator))
dirname := urlStr[:lastIndex+1]
if dirname == "" {
return "."
}
return dirname
}
示例14: isValidHostURL
// isValidHostURL - validate input host url.
func isValidHostURL(hostURL string) bool {
if strings.TrimSpace(hostURL) == "" {
return false
}
url := client.NewURL(hostURL)
if url.Scheme != "https" && url.Scheme != "http" {
return false
}
if url.Path != "" && url.Path != "/" {
return false
}
return true
}
示例15: JSON
// JSON json message for share command
func (s shareMessage) JSON() string {
var shareMessageBytes []byte
var err error
if len(s.DownloadURL) > 0 {
shareMessageBytes, err = json.Marshal(struct {
Expiry humanizedTime `json:"expiry"`
DownloadURL string `json:"downloadUrl"`
Key string `json:"keyName"`
}{
Expiry: timeDurationToHumanizedTime(s.Expiry),
DownloadURL: s.DownloadURL,
Key: s.Key,
})
} else {
var key string
URL := client.NewURL(s.Key)
postURL := URL.Scheme + URL.SchemeSeparator + URL.Host + string(URL.Separator)
if !isBucketVirtualStyle(URL.Host) {
postURL = postURL + s.UploadInfo["bucket"]
}
postURL = postURL + " "
curlCommand := "curl " + postURL
for k, v := range s.UploadInfo {
if k == "key" {
key = v
continue
}
curlCommand = curlCommand + fmt.Sprintf("-F %s=%s ", k, v)
}
curlCommand = curlCommand + fmt.Sprintf("-F key=%s ", key) + "-F [email protected]<FILE> "
shareMessageBytes, err = json.Marshal(struct {
Expiry humanizedTime `json:"expiry"`
UploadCommand string `json:"uploadCommand"`
Key string `json:"keyName"`
}{
Expiry: timeDurationToHumanizedTime(s.Expiry),
UploadCommand: curlCommand,
Key: s.Key,
})
}
fatalIf(probe.NewError(err), "Failed to marshal into JSON.")
// json encoding escapes ampersand into its unicode character which is not usable directly for share
// and fails with cloud storage. convert them back so that they are usable
shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u0026"), []byte("&"), -1)
shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u003c"), []byte("<"), -1)
shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u003e"), []byte(">"), -1)
return string(shareMessageBytes)
}