/
cli_apps.go
165 lines (142 loc) · 4.39 KB
/
cli_apps.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2015 The rkt 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.
//+build linux
package main
import (
"fmt"
"path/filepath"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/spf13/pflag"
"github.com/coreos/rkt/common/apps"
)
var (
rktApps apps.Apps // global used by run/prepare for representing the apps expressed via the cli
)
// parseApps looks through the args for support of per-app argument lists delimited with "--" and "---".
// Between per-app argument lists flags.Parse() is called using the supplied FlagSet.
// Anything not consumed by flags.Parse() and not found to be a per-app argument list is treated as an image.
// allowAppArgs controls whether "--" prefixed per-app arguments will be accepted or not.
func parseApps(al *apps.Apps, args []string, flags *pflag.FlagSet, allowAppArgs bool) error {
nAppsLastAppArgs := al.Count()
// valid args here may either be:
// not-"--"; flags handled by *flags or an image specifier
// "--"; app arguments begin
// "---"; conclude app arguments
// between "--" and "---" pairs anything is permitted.
inAppArgs := false
for i := 0; i < len(args); i++ {
a := args[i]
if inAppArgs {
switch a {
case "---":
// conclude this app's args
inAppArgs = false
default:
// keep appending to this app's args
app := al.Last()
app.Args = append(app.Args, a)
}
} else {
switch a {
case "--":
if !allowAppArgs {
return fmt.Errorf("app arguments unsupported")
}
// begin app's args
inAppArgs = true
// catch some likely mistakes
if nAppsLastAppArgs == al.Count() {
if al.Count() == 0 {
return fmt.Errorf("an image is required before any app arguments")
}
return fmt.Errorf("only one set of app arguments allowed per image")
}
nAppsLastAppArgs = al.Count()
case "---":
// ignore triple dashes since they aren't images
// TODO(vc): I don't think ignoring this is appropriate, probably should error; it implies malformed argv.
// "---" is not an image separator, it's an optional argument list terminator.
// encountering it outside of inAppArgs is likely to be "--" typoed
default:
// consume any potential inter-app flags
if err := flags.Parse(args[i:]); err != nil {
return err
}
nInterFlags := (len(args[i:]) - flags.NArg())
if nInterFlags > 0 {
// XXX(vc): flag.Parse() annoyingly consumes the "--", reclaim it here if necessary
if args[i+nInterFlags-1] == "--" {
nInterFlags--
}
// advance past what flags.Parse() consumed
i += nInterFlags - 1 // - 1 because of i++
} else {
// flags.Parse() didn't want this arg, treat as image
al.Create(a)
}
}
}
}
return nil
}
// Value interface implementations for the various per-app fields we provide flags for
// appAsc is for aci --signature
type appAsc apps.Apps
func (aa *appAsc) Set(s string) error {
app := (*apps.Apps)(aa).Last()
if app == nil {
return fmt.Errorf("--signature must follow an image")
}
if app.Asc != "" {
return fmt.Errorf("--signature specified multiple times for the same image")
}
app.Asc = s
return nil
}
func (aa *appAsc) String() string {
app := (*apps.Apps)(aa).Last()
if app == nil {
return ""
}
return app.Asc
}
func (aa *appAsc) Type() string {
return "appAsc"
}
// appExec is for aci --exec overrides
type appExec apps.Apps
func (ae *appExec) Set(s string) error {
app := (*apps.Apps)(ae).Last()
if app == nil {
return fmt.Errorf("--exec must follow an image")
}
if !filepath.IsAbs(s) {
return fmt.Errorf("--exec must be absolute path")
}
if app.Exec != "" {
return fmt.Errorf("--exec specified multiple times for the same image")
}
app.Exec = s
return nil
}
func (ae *appExec) String() string {
app := (*apps.Apps)(ae).Last()
if app == nil {
return ""
}
return app.Exec
}
func (ae *appExec) Type() string {
return "appExec"
}
// TODO(vc): --mount, --set-env, etc.