Skip to content

Commit 668fcf2

Browse files
authored
refactor: move out the storage initialize from function internal (#14)
1 parent dc0e3ed commit 668fcf2

30 files changed

+190
-113
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# modctl
22

3-
Modctl is a user-friendly CLI tool for managing OCI model artifacts, which are bundled based on Model Spec V1(https://github.com/CloudNativeAI/model-spec).
3+
Modctl is a user-friendly CLI tool for managing OCI model artifacts, which are bundled based on [Model Spec V1](https://github.com/CloudNativeAI/model-spec).
44
It offers commands such as `build`, `pull`, `push`, and more, making it easy for users to convert their AI models into OCI artifacts.
55

66
## Usage

cmd/modctl/build.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"context"
2121
"fmt"
2222

23+
"github.com/CloudNativeAI/modctl/pkg/backend"
2324
"github.com/CloudNativeAI/modctl/pkg/config"
24-
"github.com/CloudNativeAI/modctl/pkg/oci"
2525

2626
"github.com/spf13/cobra"
2727
"github.com/spf13/viper"
@@ -59,7 +59,12 @@ func init() {
5959

6060
// runBuild runs the build modctl.
6161
func runBuild(ctx context.Context, workDir string) error {
62-
if err := oci.Build(ctx, buildConfig.Modelfile, workDir, buildConfig.Target); err != nil {
62+
b, err := backend.New()
63+
if err != nil {
64+
return err
65+
}
66+
67+
if err := b.Build(ctx, buildConfig.Modelfile, workDir, buildConfig.Target); err != nil {
6368
return err
6469
}
6570

cmd/modctl/list.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import (
2222
"os"
2323
"text/tabwriter"
2424

25-
"github.com/CloudNativeAI/modctl/pkg/oci"
25+
"github.com/CloudNativeAI/modctl/pkg/backend"
26+
2627
humanize "github.com/dustin/go-humanize"
2728
"github.com/spf13/cobra"
2829
"github.com/spf13/viper"
@@ -52,7 +53,12 @@ func init() {
5253

5354
// runList runs the list modctl.
5455
func runList(ctx context.Context) error {
55-
artifacts, err := oci.List(ctx)
56+
b, err := backend.New()
57+
if err != nil {
58+
return err
59+
}
60+
61+
artifacts, err := b.List(ctx)
5662
if err != nil {
5763
return err
5864
}

cmd/modctl/login.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import (
2323
"os"
2424
"strings"
2525

26+
"github.com/CloudNativeAI/modctl/pkg/backend"
2627
"github.com/CloudNativeAI/modctl/pkg/config"
27-
"github.com/CloudNativeAI/modctl/pkg/oci"
28+
2829
"github.com/spf13/cobra"
2930
"github.com/spf13/viper"
3031
)
@@ -68,6 +69,11 @@ func init() {
6869

6970
// runLogin runs the login modctl.
7071
func runLogin(ctx context.Context, registry string) error {
72+
b, err := backend.New()
73+
if err != nil {
74+
return err
75+
}
76+
7177
// read password from stdin if password-stdin is set
7278
if loginConfig.PasswordStdin {
7379
fmt.Print("Enter password: ")
@@ -80,7 +86,7 @@ func runLogin(ctx context.Context, registry string) error {
8086
loginConfig.Password = strings.TrimSpace(password)
8187
}
8288

83-
if err := oci.Login(ctx, registry, loginConfig.Username, loginConfig.Password); err != nil {
89+
if err := b.Login(ctx, registry, loginConfig.Username, loginConfig.Password); err != nil {
8490
return err
8591
}
8692

cmd/modctl/logout.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import (
2020
"context"
2121
"fmt"
2222

23-
"github.com/CloudNativeAI/modctl/pkg/oci"
23+
"github.com/CloudNativeAI/modctl/pkg/backend"
24+
2425
"github.com/spf13/cobra"
2526
"github.com/spf13/viper"
2627
)
@@ -49,7 +50,12 @@ func init() {
4950

5051
// runLogout runs the logout modctl.
5152
func runLogout(ctx context.Context, registry string) error {
52-
if err := oci.Logout(ctx, registry); err != nil {
53+
b, err := backend.New()
54+
if err != nil {
55+
return err
56+
}
57+
58+
if err := b.Logout(ctx, registry); err != nil {
5359
return err
5460
}
5561

cmd/modctl/prune.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import (
2020
"context"
2121
"fmt"
2222

23-
"github.com/CloudNativeAI/modctl/pkg/oci"
23+
"github.com/CloudNativeAI/modctl/pkg/backend"
24+
2425
"github.com/spf13/cobra"
2526
"github.com/spf13/viper"
2627
)
@@ -49,7 +50,12 @@ func init() {
4950

5051
// runPrune runs the prune modctl.
5152
func runPrune(ctx context.Context) error {
52-
prunedBlobs, err := oci.Prune(ctx)
53+
b, err := backend.New()
54+
if err != nil {
55+
return err
56+
}
57+
58+
prunedBlobs, err := b.Prune(ctx)
5359
if err != nil {
5460
return err
5561
}

cmd/modctl/pull.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020
"context"
2121
"fmt"
2222

23+
"github.com/CloudNativeAI/modctl/pkg/backend"
2324
"github.com/CloudNativeAI/modctl/pkg/config"
24-
"github.com/CloudNativeAI/modctl/pkg/oci"
25+
2526
"github.com/spf13/cobra"
2627
"github.com/spf13/viper"
2728
)
@@ -53,16 +54,21 @@ func init() {
5354

5455
// runPull runs the pull modctl.
5556
func runPull(ctx context.Context, target string) error {
57+
b, err := backend.New()
58+
if err != nil {
59+
return err
60+
}
61+
5662
if target == "" {
5763
return fmt.Errorf("target is required")
5864
}
5965

60-
opts := []oci.Option{}
66+
opts := []backend.Option{}
6167
if pullConfig.PlainHTTP {
62-
opts = append(opts, oci.WithPlainHTTP())
68+
opts = append(opts, backend.WithPlainHTTP())
6369
}
6470

65-
if err := oci.Pull(ctx, target, opts...); err != nil {
71+
if err := b.Pull(ctx, target, opts...); err != nil {
6672
return err
6773
}
6874

cmd/modctl/push.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020
"context"
2121
"fmt"
2222

23+
"github.com/CloudNativeAI/modctl/pkg/backend"
2324
"github.com/CloudNativeAI/modctl/pkg/config"
24-
"github.com/CloudNativeAI/modctl/pkg/oci"
25+
2526
"github.com/spf13/cobra"
2627
"github.com/spf13/viper"
2728
)
@@ -53,12 +54,17 @@ func init() {
5354

5455
// runPush runs the push modctl.
5556
func runPush(ctx context.Context, target string) error {
56-
opts := []oci.Option{}
57+
b, err := backend.New()
58+
if err != nil {
59+
return err
60+
}
61+
62+
opts := []backend.Option{}
5763
if pushConfig.PlainHTTP {
58-
opts = append(opts, oci.WithPlainHTTP())
64+
opts = append(opts, backend.WithPlainHTTP())
5965
}
6066

61-
if err := oci.Push(ctx, target, opts...); err != nil {
67+
if err := b.Push(ctx, target, opts...); err != nil {
6268
return err
6369
}
6470

cmd/modctl/rm.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import (
2020
"context"
2121
"fmt"
2222

23-
"github.com/CloudNativeAI/modctl/pkg/oci"
23+
"github.com/CloudNativeAI/modctl/pkg/backend"
24+
2425
"github.com/spf13/cobra"
2526
"github.com/spf13/viper"
2627
)
@@ -49,11 +50,16 @@ func init() {
4950

5051
// runRm runs the rm modctl.
5152
func runRm(ctx context.Context, target string) error {
53+
b, err := backend.New()
54+
if err != nil {
55+
return err
56+
}
57+
5258
if target == "" {
5359
return fmt.Errorf("target is required")
5460
}
5561

56-
digest, err := oci.Remove(ctx, target)
62+
digest, err := b.Remove(ctx, target)
5763
if err != nil {
5864
return err
5965
}

pkg/backend/backend.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2024 The CNAI Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package backend
18+
19+
import (
20+
"context"
21+
22+
"github.com/CloudNativeAI/modctl/pkg/storage"
23+
)
24+
25+
// Backend is the interface to represent the backend.
26+
type Backend interface {
27+
// Login logs into a registry.
28+
Login(ctx context.Context, registry, username, password string) error
29+
30+
// Logout logs out from a registry.
31+
Logout(ctx context.Context, registry string) error
32+
33+
// Build builds the user materials into the OCI image which follows the Model Spec.
34+
Build(ctx context.Context, modelfilePath, workDir, target string) error
35+
36+
// Pull pulls an artifact from a registry.
37+
Pull(ctx context.Context, target string, opts ...Option) error
38+
39+
// Push pushes the image to the registry.
40+
Push(ctx context.Context, target string, opts ...Option) error
41+
42+
// List lists all the model artifacts.
43+
List(ctx context.Context) ([]*ModelArtifact, error)
44+
45+
// Remove deletes the model artifact.
46+
Remove(ctx context.Context, target string) (string, error)
47+
48+
// Prune prunes the unused blobs and clean up the storage.
49+
Prune(ctx context.Context) ([]string, error)
50+
}
51+
52+
// backend is the implementation of Backend.
53+
type backend struct {
54+
store storage.Storage
55+
}
56+
57+
// New creates a new backend.
58+
func New() (Backend, error) {
59+
store, err := storage.New("")
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
return &backend{
65+
store: store,
66+
}, nil
67+
}

0 commit comments

Comments
 (0)