Skip to content

Commit

Permalink
feat(cmd): impl ormb models command
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-cj committed Jan 25, 2021
1 parent 6cff6a8 commit 4522543
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
36 changes: 36 additions & 0 deletions cmd/ormb/cmd/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
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 cmd

import (
"github.com/spf13/cobra"
)

// modelsCmd represents the pull command
var modelsCmd = &cobra.Command{
Use: "models",
Short: "List localhost models",
Long: ``,
PreRunE: preRunE,
RunE: func(cmd *cobra.Command, args []string) error {

return ormbClient.Models()
},
}

func init() {
rootCmd.AddCommand(modelsCmd)
}
53 changes: 53 additions & 0 deletions pkg/oras/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io/ioutil"
"net/http"
"path"
"strconv"
"time"

auth "github.com/deislabs/oras/pkg/auth/docker"
"github.com/deislabs/oras/pkg/oras"
Expand Down Expand Up @@ -235,6 +237,57 @@ func (c *Client) LoadModel(ref *oci.Reference) (*model.Model, error) {
return r.Model, nil
}

func (c *Client) Models() error {
refs, err := c.cache.ListReferences()
if err != nil {
return err
}

maxLen := []int{0, 0, 12, 20, 0}
for _, ref := range refs {
if len(ref.Repo) > maxLen[0] {
maxLen[0] = len(ref.Repo)
}
if len(ref.Tag) > maxLen[1] {
maxLen[1] = len(ref.Tag)
}

size := bts.ByteCountBinary(ref.Size)
if len(size) > maxLen[4] {
maxLen[4] = len(size)
}
}

format := "%-" + strconv.Itoa(maxLen[0]) + "s " // repo name
format += "%-" + strconv.Itoa(maxLen[1]) + "s " // tag
format += "%-" + strconv.Itoa(maxLen[2]) + "s " // digest
format += "%-" + strconv.Itoa(maxLen[3]) + "s " // create at
format += "%-" + strconv.Itoa(maxLen[4]) + "s\n" // size
fmt.Fprintf(c.out, format, "REPOSITORY", "TAG", "MODEL ID", "CREATED", "SIZE")

for _, ref := range refs {
timeDiff := time.Now().Sub(ref.CreatedAt)
timeShow := ""
if timeDiff.Hours()/(365*24.0*7) > 1.0 {
timeShow = fmt.Sprintf("%.0f years ago", timeDiff.Hours()/(365*24.0*7))
} else if timeDiff.Hours()/(30*24.0*7) > 1.0 {
timeShow = fmt.Sprintf("%.0f months ago", timeDiff.Hours()/(30*24.0*7))
} else if timeDiff.Hours()/(24.0*7) > 1.0 {
timeShow = fmt.Sprintf("%.0f weeks ago", timeDiff.Hours()/(24.0*7))
} else if timeDiff.Hours()/24.0 > 1.0 {
timeShow = fmt.Sprintf("%.0f days ago", timeDiff.Hours()/24.0)
} else if timeDiff.Hours() > 1.0 {
timeShow = fmt.Sprintf("%.0f hours ago", timeDiff.Hours())
} else if timeDiff.Minutes() > 1.0 {
timeShow = fmt.Sprintf("%.0f minutes ago", timeDiff.Minutes())
} else if timeDiff.Seconds() > 1.0 {
timeShow = fmt.Sprintf("%.0f seconds ago", timeDiff.Seconds())
}
fmt.Fprintf(c.out, format, ref.Repo, ref.Tag, ref.Digest.Hex()[0:12], timeShow, bts.ByteCountBinary(ref.Size))
}
return nil
}

// printCacheRefSummary prints out model ref summary
func (c *Client) printCacheRefSummary(r *cache.CacheRefSummary) {
fmt.Fprintf(c.out, "ref: %s\n", r.Name)
Expand Down
1 change: 1 addition & 0 deletions pkg/oras/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type Interface interface {
PullModel(ref *oci.Reference) error
LoadModel(ref *oci.Reference) (*model.Model, error)
TagModel(ref *oci.Reference, target *oci.Reference) error
Models() error
}
5 changes: 5 additions & 0 deletions pkg/ormb/ormb.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Interface interface {
Save(src, refStr string) error
Tag(refStr, targetStr string) error
Remove(refStr string) error
Models() error
}

type ORMB struct {
Expand Down Expand Up @@ -128,3 +129,7 @@ func (o ORMB) Remove(refStr string) error {

return o.client.RemoveModel(ref)
}

func (o ORMB) Models() error {
return o.client.Models()
}
4 changes: 2 additions & 2 deletions pkg/util/bytes/byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import "fmt"
func ByteCountBinary(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
return fmt.Sprintf("%dB", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
return fmt.Sprintf("%.1f%cB", float64(b)/float64(div), "KMGTPE"[exp])
}

0 comments on commit 4522543

Please sign in to comment.