@@ -41,6 +41,11 @@ type Image struct {
4141 Names []string
4242}
4343
44+ type BuildOptions struct {
45+ Context string
46+ Tag string
47+ }
48+
4449type ImageSlice []Image
4550
4651var (
@@ -137,22 +142,25 @@ func (images ImageSlice) Swap(i, j int) {
137142 images [i ], images [j ] = images [j ], images [i ]
138143}
139144
140- func BuildImage (buildContext string ) (string , error ) {
141- if ! utils .PathExists (buildContext ) {
142- return "" , & utils.BuildError {BuildContext : buildContext , Err : ErrBuildContextDoesNotExist }
145+ func BuildImage (build BuildOptions ) (string , error ) {
146+ if ! utils .PathExists (build . Context ) {
147+ return "" , & utils.BuildError {BuildContext : build . Context , Err : ErrBuildContextDoesNotExist }
143148 }
144- if stat , err := os .Stat (buildContext ); err != nil {
149+ if stat , err := os .Stat (build . Context ); err != nil {
145150 return "" , err
146151 } else {
147152 if ! stat .Mode ().IsDir () {
148- return "" , & utils.BuildError {BuildContext : buildContext , Err : ErrBuildContextInvalid }
153+ return "" , & utils.BuildError {BuildContext : build . Context , Err : ErrBuildContextInvalid }
149154 }
150155 }
151- if ! utils .PathExists (buildContext + "/Containerfile" ) && ! utils .PathExists (buildContext + "/Dockerfile" ) {
152- return "" , & utils.BuildError {BuildContext : buildContext , Err : ErrBuildContextInvalid }
156+ if ! utils .PathExists (build . Context + "/Containerfile" ) && ! utils .PathExists (build . Context + "/Dockerfile" ) {
157+ return "" , & utils.BuildError {BuildContext : build . Context , Err : ErrBuildContextInvalid }
153158 }
154159 logLevelString := LogLevel .String ()
155- args := []string {"--log-level" , logLevelString , "build" , buildContext }
160+ args := []string {"--log-level" , logLevelString , "build" , build .Context }
161+ if build .Tag != "" {
162+ args = append (args , "--tag" , build .Tag )
163+ }
156164
157165 stdout := new (bytes.Buffer )
158166 if err := shell .Run ("podman" , nil , stdout , nil , args ... ); err != nil {
@@ -162,15 +170,19 @@ func BuildImage(buildContext string) (string, error) {
162170 imageIdBegin := strings .LastIndex (output , "\n " ) + 1
163171 imageId := output [imageIdBegin :]
164172
165- info , err := InspectImage (imageId )
166- if err != nil {
167- return "" , err
168- }
169-
170- name := "localhost/" + info ["Labels" ].(map [string ]interface {})["name" ].(string )
171- args = []string {"--log-level" , logLevelString , "tag" , imageId , name }
172- if err := shell .Run ("podman" , nil , nil , nil , args ... ); err != nil {
173- return "" , err
173+ var name string
174+ if build .Tag == "" {
175+ info , err := InspectImage (imageId )
176+ if err != nil {
177+ return "" , err
178+ }
179+ name = info ["Labels" ].(map [string ]interface {})["name" ].(string )
180+ args = []string {"--log-level" , logLevelString , "tag" , imageId , name }
181+ if err := shell .Run ("podman" , nil , nil , nil , args ... ); err != nil {
182+ return "" , err
183+ }
184+ } else {
185+ name = build .Tag
174186 }
175187
176188 return name , nil
0 commit comments