Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
4cb42ca5
Unverified
Commit
4cb42ca5
authored
Jul 24, 2023
by
Patrick Devine
Committed by
GitHub
Jul 24, 2023
Browse files
add copy command (#191)
parent
ec5e22ac
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
77 additions
and
2 deletions
+77
-2
api/client.go
api/client.go
+7
-0
api/types.go
api/types.go
+5
-0
cmd/cmd.go
cmd/cmd.go
+21
-2
server/images.go
server/images.go
+26
-0
server/routes.go
server/routes.go
+18
-0
No files found.
api/client.go
View file @
4cb42ca5
...
...
@@ -210,6 +210,13 @@ func (c *Client) List(ctx context.Context) (*ListResponse, error) {
return
&
lr
,
nil
}
func
(
c
*
Client
)
Copy
(
ctx
context
.
Context
,
req
*
CopyRequest
)
error
{
if
err
:=
c
.
do
(
ctx
,
http
.
MethodPost
,
"/api/copy"
,
req
,
nil
);
err
!=
nil
{
return
err
}
return
nil
}
func
(
c
*
Client
)
Delete
(
ctx
context
.
Context
,
req
*
DeleteRequest
)
error
{
if
err
:=
c
.
do
(
ctx
,
http
.
MethodDelete
,
"/api/delete"
,
req
,
nil
);
err
!=
nil
{
return
err
...
...
api/types.go
View file @
4cb42ca5
...
...
@@ -48,6 +48,11 @@ type DeleteRequest struct {
Name
string
`json:"name"`
}
type
CopyRequest
struct
{
Source
string
`json:"source"`
Destination
string
`json:"destination"`
}
type
PullRequest
struct
{
Name
string
`json:"name"`
Insecure
bool
`json:"insecure,omitempty"`
...
...
cmd/cmd.go
View file @
4cb42ca5
...
...
@@ -155,14 +155,25 @@ func ListHandler(cmd *cobra.Command, args []string) error {
func
DeleteHandler
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
client
:=
api
.
NewClient
()
req
uest
:=
api
.
DeleteRequest
{
Name
:
args
[
0
]}
if
err
:=
client
.
Delete
(
context
.
Background
(),
&
req
uest
);
err
!=
nil
{
req
:=
api
.
DeleteRequest
{
Name
:
args
[
0
]}
if
err
:=
client
.
Delete
(
context
.
Background
(),
&
req
);
err
!=
nil
{
return
err
}
fmt
.
Printf
(
"deleted '%s'
\n
"
,
args
[
0
])
return
nil
}
func
CopyHandler
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
client
:=
api
.
NewClient
()
req
:=
api
.
CopyRequest
{
Source
:
args
[
0
],
Destination
:
args
[
1
]}
if
err
:=
client
.
Copy
(
context
.
Background
(),
&
req
);
err
!=
nil
{
return
err
}
fmt
.
Printf
(
"copied '%s' to '%s'
\n
"
,
args
[
0
],
args
[
1
])
return
nil
}
func
PullHandler
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
insecure
,
err
:=
cmd
.
Flags
()
.
GetBool
(
"insecure"
)
if
err
!=
nil
{
...
...
@@ -470,6 +481,13 @@ func NewCLI() *cobra.Command {
RunE
:
ListHandler
,
}
copyCmd
:=
&
cobra
.
Command
{
Use
:
"cp"
,
Short
:
"Copy a model"
,
Args
:
cobra
.
MinimumNArgs
(
2
),
RunE
:
CopyHandler
,
}
deleteCmd
:=
&
cobra
.
Command
{
Use
:
"rm"
,
Short
:
"Remove a model"
,
...
...
@@ -484,6 +502,7 @@ func NewCLI() *cobra.Command {
pullCmd
,
pushCmd
,
listCmd
,
copyCmd
,
deleteCmd
,
)
...
...
server/images.go
View file @
4cb42ca5
...
...
@@ -493,6 +493,32 @@ func CreateLayer(f io.ReadSeeker) (*LayerReader, error) {
return
layer
,
nil
}
func
CopyModel
(
src
,
dest
string
)
error
{
srcPath
,
err
:=
ParseModelPath
(
src
)
.
GetManifestPath
(
false
)
if
err
!=
nil
{
return
err
}
destPath
,
err
:=
ParseModelPath
(
dest
)
.
GetManifestPath
(
true
)
if
err
!=
nil
{
return
err
}
// copy the file
input
,
err
:=
ioutil
.
ReadFile
(
srcPath
)
if
err
!=
nil
{
fmt
.
Println
(
"Error reading file:"
,
err
)
return
err
}
err
=
ioutil
.
WriteFile
(
destPath
,
input
,
0644
)
if
err
!=
nil
{
fmt
.
Println
(
"Error reading file:"
,
err
)
return
err
}
return
nil
}
func
DeleteModel
(
name
string
)
error
{
mp
:=
ParseModelPath
(
name
)
...
...
server/routes.go
View file @
4cb42ca5
...
...
@@ -228,6 +228,23 @@ func ListModelsHandler(c *gin.Context) {
c
.
JSON
(
http
.
StatusOK
,
api
.
ListResponse
{
models
})
}
func
CopyModelHandler
(
c
*
gin
.
Context
)
{
var
req
api
.
CopyRequest
if
err
:=
c
.
ShouldBindJSON
(
&
req
);
err
!=
nil
{
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"error"
:
err
.
Error
()})
return
}
if
err
:=
CopyModel
(
req
.
Source
,
req
.
Destination
);
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
c
.
JSON
(
http
.
StatusNotFound
,
gin
.
H
{
"error"
:
fmt
.
Sprintf
(
"model '%s' not found"
,
req
.
Source
)})
}
else
{
c
.
JSON
(
http
.
StatusInternalServerError
,
gin
.
H
{
"error"
:
err
.
Error
()})
}
return
}
}
func
Serve
(
ln
net
.
Listener
)
error
{
config
:=
cors
.
DefaultConfig
()
config
.
AllowWildcard
=
true
...
...
@@ -254,6 +271,7 @@ func Serve(ln net.Listener) error {
r
.
POST
(
"/api/generate"
,
GenerateHandler
)
r
.
POST
(
"/api/create"
,
CreateModelHandler
)
r
.
POST
(
"/api/push"
,
PushModelHandler
)
r
.
POST
(
"/api/copy"
,
CopyModelHandler
)
r
.
GET
(
"/api/tags"
,
ListModelsHandler
)
r
.
DELETE
(
"/api/delete"
,
DeleteModelHandler
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment