本文整理汇总了Golang中github.com/appc/spec/discovery.NewAppFromString函数的典型用法代码示例。如果您正苦于以下问题:Golang NewAppFromString函数的具体用法?Golang NewAppFromString怎么用?Golang NewAppFromString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewAppFromString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseApp
func parseApp(args []string) ([]string, *schema.RuntimeApp, error) {
if len(args) == 0 {
return nil, nil, nil
}
rtapp := schema.RuntimeApp{}
// Parse first argument (image name)
if h, err := types.NewHash(args[0]); err == nil {
rtapp.Image.ID = *h
rtapp.Name.Set(h.String()) // won't err
} else if dapp, err := discovery.NewAppFromString(args[0]); err == nil {
rtapp.Image.Name = &dapp.Name
rtapp.Name.Set(path.Base(dapp.Name.String())) // won't err here
if ll, err := types.LabelsFromMap(dapp.Labels); err != nil {
return args, nil, err
} else {
rtapp.Image.Labels = ll
}
} else {
return args, nil, err
}
fl := flag.NewFlagSet(args[0], flag.ExitOnError)
fl.Var(&rtapp.Name, "name", "App name")
fl.Var((*AnnotationsFlag)(&rtapp.Annotations), "a", "Add annotation (NAME=VALUE)")
fl.Var((*MountsFlag)(&rtapp.Mounts), "m", "Mount volume (VOLUME[:MOUNTPOINT])")
// TODO: app override
fl.Parse(args[1:])
return fl.Args(), &rtapp, nil
}
示例2: runDiscover
func runDiscover(args []string) (exit int) {
if len(args) < 1 {
stderr("discover: at least one name required")
}
for _, name := range args {
app, err := discovery.NewAppFromString(name)
if app.Labels["os"] == "" {
app.Labels["os"] = runtime.GOOS
}
if app.Labels["arch"] == "" {
app.Labels["arch"] = runtime.GOARCH
}
if err != nil {
stderr("%s: %s", name, err)
return 1
}
eps, attempts, err := discovery.DiscoverEndpoints(*app, transportFlags.Insecure)
if err != nil {
stderr("error fetching %s: %s", name, err)
return 1
}
for _, a := range attempts {
fmt.Printf("discover walk: prefix: %s error: %v\n", a.Prefix, a.Error)
}
for _, aciEndpoint := range eps.ACIEndpoints {
fmt.Printf("ACI: %s, ASC: %s\n", aciEndpoint.ACI, aciEndpoint.ASC)
}
if len(eps.Keys) > 0 {
fmt.Println("Keys: " + strings.Join(eps.Keys, ","))
}
}
return
}
示例3: LatestVersion
func (n ACFullname) LatestVersion() (string, error) {
app, err := discovery.NewAppFromString(n.Name() + ":latest")
if app.Labels["os"] == "" {
app.Labels["os"] = "linux"
}
if app.Labels["arch"] == "" {
app.Labels["arch"] = "amd64"
}
endpoint, _, err := discovery.DiscoverEndpoints(*app, nil, false)
if err != nil {
return "", errors.Annotate(err, "Latest discovery fail")
}
r, _ := regexp.Compile(`^(\d+\.)?(\d+\.)?(\*|\d+)(\-[\dA-Za-z]+){0,1}$`)
url := getRedirectForLatest(endpoint.ACIEndpoints[0].ACI)
logs.WithField("url", url).Debug("latest verion url")
for _, part := range strings.Split(url, "/") {
if r.Match([]byte(part)) {
return part, nil
}
}
return "", errors.New("No latest version found")
}
示例4: metaDiscoverPubKeyLocations
// metaDiscoverPubKeyLocations discovers the locations of public keys through ACDiscovery by applying prefix as an ACApp
func (m *Manager) metaDiscoverPubKeyLocations(prefix string) ([]string, error) {
app, err := discovery.NewAppFromString(prefix)
if err != nil {
return nil, err
}
hostHeaders := config.ResolveAuthPerHost(m.AuthPerHost)
insecure := discovery.InsecureNone
if m.InsecureAllowHTTP {
insecure = insecure | discovery.InsecureHttp
}
if m.InsecureSkipTLSCheck {
insecure = insecure | discovery.InsecureTls
}
ep, attempts, err := discovery.DiscoverPublicKeys(*app, hostHeaders, insecure)
if err != nil {
return nil, err
}
if m.Debug {
for _, a := range attempts {
log.PrintE(fmt.Sprintf("meta tag 'ac-discovery-pubkeys' not found on %s", a.Prefix), a.Error)
}
}
return ep.Keys, nil
}
示例5: LatestVersion
func (n ACFullname) LatestVersion() (string, error) {
app, err := discovery.NewAppFromString(n.Name() + ":latest")
if app.Labels["os"] == "" {
app.Labels["os"] = "linux"
}
if app.Labels["arch"] == "" {
app.Labels["arch"] = "amd64"
}
endpoints, _, err := discovery.DiscoverACIEndpoints(*app, nil, discovery.InsecureTLS|discovery.InsecureHTTP) //TODO support security
if err != nil {
return "", errors.Annotate(err, "Latest discovery fail")
}
r, _ := regexp.Compile(`^\d+(.\d+){0,2}(-[\.\-\dA-Za-z]+){0,1}$`) // TODO this is nexus specific
if len(endpoints) == 0 {
return "", errs.WithF(data.WithField("aci", string(n)), "Discovery does not give an endpoint to check latest version")
}
url := getRedirectForLatest(endpoints[0].ACI)
logs.WithField("url", url).Debug("latest verion url")
for _, part := range strings.Split(url, "/") {
if r.Match([]byte(part)) {
return part, nil
}
}
return "", errors.New("No latest version found")
}
示例6: TestNewAppcFromAppString
func TestNewAppcFromAppString(t *testing.T) {
tests := []struct {
appcRef string
expected string
}{
{
"example.com/app01",
"cimd:appc:v=0:example.com/app01",
},
{
"example.com/app01:v1.0.0",
"cimd:appc:v=0:example.com/app01?version=v1.0.0",
},
{
"example.com/app01,version=v1.0.0",
"cimd:appc:v=0:example.com/app01?version=v1.0.0",
},
{
"example.com/app01,version=v1.0.0,label01=?&*/",
"cimd:appc:v=0:example.com/app01?label01=%3F%26%2A%2F&version=v1.0.0",
},
{
"example-app01",
"cimd:appc:v=0:example-app01",
},
{
"example-app01:v1.0",
"cimd:appc:v=0:example-app01?version=v1.0",
},
}
for _, tt := range tests {
app, err := discovery.NewAppFromString(tt.appcRef)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
appc := NewAppcFromApp(app)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
u, err := url.Parse(tt.expected)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
td, err := NewAppc(u)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !appc.Equals(td) {
t.Errorf("expected identical distribution but got %q != %q", td.CIMD().String(), appc.CIMD().String())
continue
}
}
}
示例7: TestApp
func TestApp(t *testing.T) {
tests := []struct {
uriStr string
out string
}{
{
"cimd:appc:v=0:example.com/app01",
"example.com/app01",
},
{
"cimd:appc:v=0:example.com/app01?version=v1.0.0",
"example.com/app01:v1.0.0",
},
{
"cimd:appc:v=0:example.com/app01?version=v1.0.0",
"example.com/app01,version=v1.0.0",
},
{
"cimd:appc:v=0:example.com/app01?label01=%3F%26%2A%2F&version=v1.0.0",
"example.com/app01,version=v1.0.0,label01=?&*/",
},
{
"cimd:appc:v=0:example-app01",
"example-app01",
},
{
"cimd:appc:v=0:example-app01?version=v1.0",
"example-app01:v1.0",
},
}
for _, tt := range tests {
u, err := url.Parse(tt.uriStr)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
appc, err := NewAppc(u)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
app := appc.(*Appc).App()
expectedApp, err := discovery.NewAppFromString(tt.out)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(app, expectedApp) {
t.Fatalf("expected app %s, but got %q", expectedApp.String(), app.String())
}
}
}
示例8: RetrieveImage
// RetrieveImage can be used to retrieve a remote image, and optionally discover
// an image based on the App Container Image Discovery specification. Supports
// handling local images as well as
func RetrieveImage(imageUri string, insecure bool) (ReaderCloserSeeker, error) {
u, err := url.Parse(imageUri)
if err != nil {
return nil, err
}
switch u.Scheme {
case "file":
// for file:// urls, just load the file and return it
return os.Open(u.Path)
case "http", "https":
// Handle HTTP retrievals, wrapped with a tempfile that cleans up.
resp, err := Client.Get(imageUri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
default:
return nil, fmt.Errorf("HTTP %d on retrieving %q", resp.StatusCode, imageUri)
}
return newTempReader(resp.Body)
case "":
app, err := discovery.NewAppFromString(imageUri)
if err != nil {
return nil, err
}
endpoints, _, err := discovery.DiscoverEndpoints(*app, insecure)
if err != nil {
return nil, err
}
for _, ep := range endpoints.ACIEndpoints {
r, err := RetrieveImage(ep.ACI, insecure)
if err != nil {
continue
}
// FIXME should also attempt to validate the signature
return r, nil
}
return nil, fmt.Errorf("failed to find a valid image for %q", imageUri)
default:
return nil, fmt.Errorf("%q scheme not supported", u.Scheme)
}
}
示例9: runAddDep
func runAddDep(cmd *cobra.Command, args []string) (exit int) {
if len(args) == 0 {
cmd.Usage()
return 1
}
if len(args) != 1 {
stderr("dependency add: incorrect number of arguments")
return 1
}
if debug {
stderr("Adding dependency %q", args[0])
}
app, err := discovery.NewAppFromString(args[0])
if err != nil {
stderr("dependency add: couldn't parse dependency name: %v", err)
return 1
}
appcLabels := types.Labels(labels)
for name, value := range app.Labels {
if _, ok := appcLabels.Get(string(name)); ok {
stderr("multiple %s labels specified", name)
return 1
}
appcLabels = append(appcLabels, types.Label{
Name: name,
Value: value,
})
}
var hash *types.Hash
if imageId != "" {
var err error
hash, err = types.NewHash(imageId)
if err != nil {
stderr("dependency add: couldn't parse image ID: %v", err)
return 1
}
}
err = newACBuild().AddDependency(app.Name, hash, appcLabels, size)
if err != nil {
stderr("dependency add: %v", err)
return getErrorCode(err)
}
return 0
}
示例10: tryAppFromString
func tryAppFromString(location string) *discovery.App {
if app, err := discovery.NewAppFromString(location); err != nil {
return nil
} else {
if app.Labels["os"] == "" {
app.Labels["os"] = runtime.GOOS
}
if app.Labels["arch"] == "" {
app.Labels["arch"] = runtime.GOARCH
}
return app
}
}
示例11: runDiscover
func runDiscover(args []string) (exit int) {
if len(args) < 1 {
stderr("discover: at least one name required")
}
for _, name := range args {
app, err := discovery.NewAppFromString(name)
if app.Labels["os"] == "" {
app.Labels["os"] = runtime.GOOS
}
if app.Labels["arch"] == "" {
app.Labels["arch"] = runtime.GOARCH
}
if err != nil {
stderr("%s: %s", name, err)
return 1
}
insecure := discovery.InsecureNone
if transportFlags.Insecure {
insecure = discovery.InsecureTls | discovery.InsecureHttp
}
eps, attempts, err := discovery.DiscoverEndpoints(*app, nil, insecure)
if err != nil {
stderr("error fetching %s: %s", name, err)
return 1
}
for _, a := range attempts {
fmt.Printf("discover walk: prefix: %s error: %v\n", a.Prefix, a.Error)
}
if outputJson {
jsonBytes, err := json.MarshalIndent(&eps, "", " ")
if err != nil {
stderr("error generating JSON: %s", err)
return 1
}
fmt.Println(string(jsonBytes))
} else {
for _, aciEndpoint := range eps.ACIEndpoints {
fmt.Printf("ACI: %s, ASC: %s\n", aciEndpoint.ACI, aciEndpoint.ASC)
}
if len(eps.Keys) > 0 {
fmt.Println("Keys: " + strings.Join(eps.Keys, ","))
}
}
}
return
}
示例12: discoverPod
func (s Service) discoverPod(name cntspec.ACFullname) []cntspec.ACFullname {
logAci := s.log.WithField("pod", name)
app, err := discovery.NewAppFromString(name.String())
if app.Labels["os"] == "" {
app.Labels["os"] = "linux"
}
if app.Labels["arch"] == "" {
app.Labels["arch"] = "amd64"
}
endpoint, _, err := discovery.DiscoverEndpoints(*app, false)
if err != nil {
logAci.WithError(err).Fatal("pod discovery failed")
}
url := endpoint.ACIEndpoints[0].ACI
url = strings.Replace(url, "=aci", "=pod", 1) // TODO this is nexus specific
logUrl := logAci.WithField("url", url)
response, err := http.Get(url)
if err != nil {
logUrl.WithError(err).Fatal("Cannot get pod manifest content")
return nil
} else {
if response.StatusCode != 200 {
logUrl.WithField("status_code", response.StatusCode).WithField("status_message", response.Status).
Fatal("Receive response error for discovery")
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
logUrl.WithError(err).Fatal("Cannot read pod manifest content")
}
tmpMap := make(map[string][]cntspec.ACFullname, 1)
if err := s.podManifestToMap(tmpMap, content); err != nil {
logUrl.WithError(err).Fatal("Cannot read pod content")
}
acis := tmpMap[name.Name()]
if acis == nil {
logUrl.Fatal("Discovered pod name does not match requested")
}
return acis
}
}
示例13: getStoreKeyFromApp
func getStoreKeyFromApp(s *imagestore.Store, img string) (string, error) {
app, err := discovery.NewAppFromString(img)
if err != nil {
return "", errwrap.Wrap(fmt.Errorf("cannot parse the image name %q", img), err)
}
labels, err := types.LabelsFromMap(app.Labels)
if err != nil {
return "", errwrap.Wrap(fmt.Errorf("invalid labels in the image %q", img), err)
}
key, err := s.GetACI(app.Name, labels)
if err != nil {
switch err.(type) {
case imagestore.ACINotFoundError:
return "", err
default:
return "", errwrap.Wrap(fmt.Errorf("cannot find image %q", img), err)
}
}
return key, nil
}
示例14: parseImageName
func parseImageName(name string) (types.ACIdentifier, types.Labels, error) {
app, err := discovery.NewAppFromString(name)
if err != nil {
return "", nil, errors.Trace(err)
}
if app.Labels["os"] == "" {
app.Labels["os"] = runtime.GOOS
}
if app.Labels["arch"] == "" {
app.Labels["arch"] = runtime.GOARCH
}
labels, err := types.LabelsFromMap(app.Labels)
if err != nil {
return "", nil, errors.Trace(err)
}
return app.Name, labels, nil
}
示例15: newAppBundle
func newAppBundle(name string) (*appBundle, error) {
app, err := discovery.NewAppFromString(name)
if err != nil {
return nil, errwrap.Wrap(fmt.Errorf("invalid image name %q", name), err)
}
if _, ok := app.Labels["arch"]; !ok {
app.Labels["arch"] = runtime.GOARCH
}
if _, ok := app.Labels["os"]; !ok {
app.Labels["os"] = runtime.GOOS
}
if err := types.IsValidOSArch(app.Labels, stage0.ValidOSArch); err != nil {
return nil, errwrap.Wrap(fmt.Errorf("invalid image name %q", name), err)
}
bundle := &appBundle{
App: app,
Str: name,
}
return bundle, nil
}