Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
add aci-version command to get version of an aci file
Browse files Browse the repository at this point in the history
  • Loading branch information
n0rad committed Dec 2, 2015
1 parent 6da9174 commit 8c4ffbe
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 51 deletions.
49 changes: 1 addition & 48 deletions builder/aci-push.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package builder

import (
"github.com/appc/spec/aci"
"github.com/appc/spec/schema"
"github.com/blablacar/cnt/config"
"github.com/blablacar/cnt/utils"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)

Expand All @@ -25,7 +19,7 @@ func (aci *Aci) Push() {

aci.tarAci(true)

im := extractManifestFromAci(aci.target + PATH_IMAGE_ACI_ZIP)
im := utils.ExtractManifestFromAci(aci.target + PATH_IMAGE_ACI_ZIP)
val, _ := im.Labels.Get("version")
if err := utils.ExecCmd("curl", "-f", "-i",
"-F", "r=releases",
Expand All @@ -41,44 +35,3 @@ func (aci *Aci) Push() {
panic("Cannot push aci" + err.Error())
}
}

func extractManifestFromAci(aciPath string) schema.ImageManifest {
input, err := os.Open(aciPath)
if err != nil {
panic("cat-manifest: Cannot open %s: %v" + aciPath + err.Error())
}
defer input.Close()

tr, err := aci.NewCompressedTarReader(input)
if err != nil {
panic("cat-manifest: Cannot open tar %s: %v" + aciPath + err.Error())
}

im := schema.ImageManifest{}

Tar:
for {
hdr, err := tr.Next()
switch err {
case io.EOF:
break Tar
case nil:
if filepath.Clean(hdr.Name) == aci.ManifestFile {
bytes, err := ioutil.ReadAll(tr)
if err != nil {
panic(err)
}

err = im.UnmarshalJSON(bytes)
if err != nil {
panic(err)
}
return im
}
default:
panic("error reading tarball: %v" + err.Error())
}
}
panic("Cannot found manifest if aci")
return im
}
21 changes: 21 additions & 0 deletions commands/aci-version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package commands

import (
"github.com/blablacar/cnt/utils"
"github.com/spf13/cobra"
"os"
)

var aciVersion = &cobra.Command{
Use: "aci-version file",
Short: "display version of aci",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.Usage()
os.Exit(1)
}
im := utils.ExtractManifestFromAci(args[0])
val, _ := im.Labels.Get("version")
println(val)
},
}
6 changes: 3 additions & 3 deletions commands/cnt.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func Execute() {
}
rootCmd.PersistentFlags().BoolVarP(&buildArgs.Clean, "clean", "c", false, "Clean before doing anything")
rootCmd.PersistentFlags().StringVarP(&buildArgs.TargetsRootPath, "targets-root-path", "p", "", "Set targets root path")
rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "L", "debug", "Set log level")
rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "L", "info", "Set log level")

rootCmd.AddCommand(buildCmd, cleanCmd, pushCmd, installCmd, testCmd, versionCmd, initCmd, updateCmd, graphCmd)
rootCmd.AddCommand(buildCmd, cleanCmd, pushCmd, installCmd, testCmd, versionCmd, initCmd, updateCmd, graphCmd, aciVersion)

config.GetConfig().Load()

Expand All @@ -46,7 +46,7 @@ func Execute() {

rootCmd.Execute()

log.Info("Victory !")
log.Debug("Victory !")
}

func checkRktVersion() {
Expand Down
51 changes: 51 additions & 0 deletions utils/aci-utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package utils

import (
"github.com/appc/spec/aci"
"github.com/appc/spec/schema"
"io"
"io/ioutil"
"os"
"path/filepath"
)

func ExtractManifestFromAci(aciPath string) schema.ImageManifest {
input, err := os.Open(aciPath)
if err != nil {
panic("cat-manifest: Cannot open %s: %v" + aciPath + err.Error())
}
defer input.Close()

tr, err := aci.NewCompressedTarReader(input)
if err != nil {
panic("cat-manifest: Cannot open tar %s: %v" + aciPath + err.Error())
}

im := schema.ImageManifest{}

Tar:
for {
hdr, err := tr.Next()
switch err {
case io.EOF:
break Tar
case nil:
if filepath.Clean(hdr.Name) == aci.ManifestFile {
bytes, err := ioutil.ReadAll(tr)
if err != nil {
panic(err)
}

err = im.UnmarshalJSON(bytes)
if err != nil {
panic(err)
}
return im
}
default:
panic("error reading tarball: %v" + err.Error())
}
}
panic("Cannot found manifest if aci")
return im
}

0 comments on commit 8c4ffbe

Please sign in to comment.