Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd): impl ormb models command #172

Merged
merged 2 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.0
github.com/stretchr/testify v1.5.1 // indirect
github.com/xeonx/timeago v1.0.0-rc4
github.com/yvasiyarov/go-metrics v0.0.0-20150112132944-c25f46c4b940 // indirect
github.com/yvasiyarov/gorelic v0.0.7 // indirect
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20160601141957-9c099fbc30e9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
github.com/xeonx/timeago v1.0.0-rc4 h1:9rRzv48GlJC0vm+iBpLcWAr8YbETyN9Vij+7h2ammz4=
github.com/xeonx/timeago v1.0.0-rc4/go.mod h1:qDLrYEFynLO7y5Ho7w3GwgtYgpy5UfhcXIIQvMKVDkA=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
39 changes: 38 additions & 1 deletion pkg/oras/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"io/ioutil"
"net/http"
"path"
"strconv"

auth "github.com/deislabs/oras/pkg/auth/docker"
"github.com/deislabs/oras/pkg/oras"
"github.com/kleveross/ormb/pkg/consts"
"github.com/kleveross/ormb/pkg/model"
Expand All @@ -19,7 +19,10 @@ import (
bts "github.com/kleveross/ormb/pkg/util/bytes"
"github.com/kleveross/ormb/pkg/util/ctx"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"

auth "github.com/deislabs/oras/pkg/auth/docker"
"github.com/pkg/errors"
"github.com/xeonx/timeago"
)

const (
Expand Down Expand Up @@ -235,6 +238,40 @@ 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 {
fmt.Fprintf(c.out, format, ref.Repo, ref.Tag, ref.Digest.Hex()[0:12], timeago.English.Format(ref.CreatedAt), 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
}
14 changes: 14 additions & 0 deletions pkg/oras/mock/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions pkg/ormb/mock/mock_ormb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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])
}
1 change: 1 addition & 0 deletions vendor/github.com/xeonx/timeago/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/xeonx/timeago/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vendor/github.com/xeonx/timeago/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading