1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// Copyright 2015 The appc Authors
//
// 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 discovery
import (
"fmt"
"net/url"
"strings"
"github.com/appc/spec/schema/common"
"github.com/appc/spec/schema/types"
)
type App struct {
Name types.ACIdentifier
Labels map[types.ACIdentifier]string
}
func NewApp(name string, labels map[types.ACIdentifier]string) (*App, error) {
if labels == nil {
labels = make(map[types.ACIdentifier]string, 0)
}
acn, err := types.NewACIdentifier(name)
if err != nil {
return nil, err
}
return &App{
Name: *acn,
Labels: labels,
}, nil
}
// NewAppFromString takes a command line app parameter and returns a map of labels.
//
// Example app parameters:
// example.com/reduce-worker:1.0.0
// example.com/reduce-worker,channel=alpha,label=value
// example.com/reduce-worker:1.0.0,label=value
//
// As can be seen in above examples - colon, comma and equal sign have
// special meaning. If any of them has to be a part of a label's value
// then consider writing your own string to App parser.
func NewAppFromString(app string) (*App, error) {
var (
name string
labels map[types.ACIdentifier]string
)
preparedApp, err := prepareAppString(app)
if err != nil {
return nil, err
}
v, err := url.ParseQuery(preparedApp)
if err != nil {
return nil, err
}
labels = make(map[types.ACIdentifier]string, 0)
for key, val := range v {
if len(val) > 1 {
return nil, fmt.Errorf("label %s with multiple values %q", key, val)
}
if key == "name" {
name = val[0]
continue
}
labelName, err := types.NewACIdentifier(key)
if err != nil {
return nil, err
}
labels[*labelName] = val[0]
}
a, err := NewApp(name, labels)
if err != nil {
return nil, err
}
return a, nil
}
func prepareAppString(app string) (string, error) {
if err := checkColon(app); err != nil {
return "", err
}
app = "name=" + strings.Replace(app, ":", ",version=", 1)
return common.MakeQueryString(app)
}
func checkColon(app string) error {
firstComma := strings.IndexRune(app, ',')
firstColon := strings.IndexRune(app, ':')
if firstColon > firstComma && firstComma > -1 {
return fmt.Errorf("malformed app string - colon may appear only right after the app name")
}
if strings.Count(app, ":") > 1 {
return fmt.Errorf("malformed app string - colon may appear at most once")
}
return nil
}
func (a *App) Copy() *App {
ac := &App{
Name: a.Name,
Labels: make(map[types.ACIdentifier]string, 0),
}
for k, v := range a.Labels {
ac.Labels[k] = v
}
return ac
}
// String returns the URL-like image name
func (a *App) String() string {
img := a.Name.String()
for n, v := range a.Labels {
img += fmt.Sprintf(",%s=%s", n, v)
}
return img
}
|