11package oci
22
33import (
4- "bytes"
54 "encoding/json"
65 "fmt"
76 "os"
3231func init () {
3332 OCICmd .Flags ().StringVarP (& platform , "platform" , "p" , "" , "The platform to build the image for" )
3433 OCICmd .Flags ().StringVarP (& output , "output" , "o" , "" , "location of the build artifacts generated" )
35- OCICmd .Flags ().StringVarP (& tag , "tag" , "t" , "" , "Tag for the OCI image" )
3634 OCICmd .Flags ().BoolVarP (& loadDocker , "load-docker" , "" , false , "Load the image into docker daemon" )
3735 OCICmd .Flags ().BoolVarP (& loadPodman , "load-podman" , "" , false , "Load the image into podman" )
3836 OCICmd .Flags ().BoolVarP (& push , "push" , "" , false , "Push the image to the registry" )
@@ -58,34 +56,40 @@ var OCICmd = &cobra.Command{
5856 os .Exit (1 )
5957 }
6058
61- artifact , p , err := ProcessPlatformAndConfig ( platform , args [ 0 ] )
59+ conf , err := hcl2nix . ReadHclFile ( "bsf.hcl" )
6260 if err != nil {
6361 fmt .Println (styles .ErrorStyle .Render ("error: " , err .Error ()))
6462 os .Exit (1 )
6563 }
64+
65+ artifact , p , err := ProcessPlatformAndConfig (conf , platform , args [0 ])
66+ if err != nil {
67+ fmt .Println (styles .ErrorStyle .Render ("error: " , err .Error ()))
68+ os .Exit (1 )
69+ }
70+
6671 platform = p
67- nameMap := make (map [string ]string )
68- originalName := artifact .Name
69- var newName string
70-
71- if tag != "" {
72- if strings .Contains (artifact .Name , ":" ) {
73- parts := strings .Split (artifact .Name , ":" )
74- if len (parts ) > 0 {
75- newName = fmt .Sprintf ("%s:%s" , parts [0 ], tag )
76- } else {
77- newName = fmt .Sprintf ("%s:%s" , artifact .Name , tag )
78- }
79- } else {
80- newName = fmt .Sprintf ("%s:%s" , artifact .Name , tag )
72+
73+ if tag != "" && ! dfSwap {
74+ newName , err := getNewName (artifact , tag )
75+ if err != nil {
76+ fmt .Println (styles .ErrorStyle .Render ("error: " , err .Error ()))
77+ os .Exit (1 )
78+ }
79+ oldName := artifact .Name
80+ artifact .Name = newName
81+ err = hcl2nix .ModifyConfig (oldName , artifact , conf )
82+ if err != nil {
83+ fmt .Println (styles .ErrorStyle .Render ("error: " , err .Error ()))
84+ os .Exit (1 )
8185 }
8286 }
83- nameMap [originalName ] = newName
8487
8588 if dfSwap {
8689 if tag != "" {
8790 if err = modifyDockerfileWithTag (path , tag , devDeps ); err != nil {
8891 fmt .Println (styles .ErrorStyle .Render ("error: " , err .Error ()))
92+ os .Exit (1 )
8993 }
9094 fmt .Println (styles .SucessStyle .Render ("dockerfile succesfully updated with tag:" , tag ))
9195 } else {
@@ -99,7 +103,7 @@ var OCICmd = &cobra.Command{
99103 fmt .Println (styles .ErrorStyle .Render ("error: " , err .Error ()))
100104 os .Exit (1 )
101105 }
102- err = generate .Generate (fh , sc , nameMap )
106+ err = generate .Generate (fh , sc )
103107
104108 if err != nil {
105109 fmt .Println (styles .ErrorStyle .Render ("error: " , err .Error ()))
@@ -219,7 +223,7 @@ var OCICmd = &cobra.Command{
219223}
220224
221225// ProcessPlatformAndConfig processes the platform and config file
222- func ProcessPlatformAndConfig (plat string , envName string ) (hcl2nix.OCIArtifact , string , error ) {
226+ func ProcessPlatformAndConfig (conf * hcl2nix. Config , plat string , envName string ) (hcl2nix.OCIArtifact , string , error ) {
223227 if plat == "" {
224228 tos , tarch := platformutils .FindPlatform (plat )
225229 plat = tos + "/" + tarch
@@ -235,16 +239,6 @@ func ProcessPlatformAndConfig(plat string, envName string) (hcl2nix.OCIArtifact,
235239 if ! pfound {
236240 return hcl2nix.OCIArtifact {}, "" , fmt .Errorf ("Platform %s is not supported. Supported platforms are %s" , platform , strings .Join (supportedPlatforms , ", " ))
237241 }
238- data , err := os .ReadFile ("bsf.hcl" )
239- if err != nil {
240- return hcl2nix.OCIArtifact {}, "" , fmt .Errorf ("error: %s" , err .Error ())
241- }
242-
243- var dstErr bytes.Buffer
244- conf , err := hcl2nix .ReadConfig (data , & dstErr )
245- if err != nil {
246- return hcl2nix.OCIArtifact {}, "" , fmt .Errorf (dstErr .String ())
247- }
248242
249243 envNames := make ([]string , 0 , len (conf .OCIArtifact ))
250244 var found bool
@@ -297,6 +291,21 @@ func modifyDockerfileWithTag(path, tag string, devDeps bool) error {
297291 return nil
298292}
299293
294+ func getNewName (artifact hcl2nix.OCIArtifact , tag string ) (string , error ) {
295+ var newName string
296+ if strings .Contains (artifact .Name , ":" ) {
297+ parts := strings .Split (artifact .Name , ":" )
298+ if len (parts ) > 0 {
299+ newName = fmt .Sprintf ("%s:%s" , parts [0 ], tag )
300+ } else {
301+ newName = fmt .Sprintf ("%s:%s" , artifact .Name , tag )
302+ }
303+ } else {
304+ newName = fmt .Sprintf ("%s:%s" , artifact .Name , tag )
305+ }
306+ return newName , nil
307+ }
308+
300309func genOCIAttrName (env , platform string , artifact hcl2nix.OCIArtifact ) string {
301310 var arch string
302311
0 commit comments