allow relative paths in FROM instruction

This commit is contained in:
Jeffrey Morgan 2023-07-19 21:55:15 -07:00
parent e4d7f3e287
commit 2d305fa99a
2 changed files with 23 additions and 28 deletions

View file

@ -137,21 +137,14 @@ func GetModel(name string) (*Model, error) {
return model, nil return model, nil
} }
func getAbsPath(fp string) (string, error) { func CreateModel(name string, path string, fn func(status string)) error {
if strings.HasPrefix(fp, "~/") { mf, err := os.Open(path)
parts := strings.Split(fp, "/") if err != nil {
home, err := os.UserHomeDir() fn(fmt.Sprintf("couldn't open modelfile '%s'", path))
if err != nil { return fmt.Errorf("failed to open file: %w", err)
return "", err
}
fp = filepath.Join(home, filepath.Join(parts[1:]...))
} }
defer mf.Close()
return os.ExpandEnv(fp), nil
}
func CreateModel(name string, mf io.Reader, fn func(status string)) error {
fn("parsing modelfile") fn("parsing modelfile")
commands, err := parser.Parse(mf) commands, err := parser.Parse(mf)
if err != nil { if err != nil {
@ -169,11 +162,22 @@ func CreateModel(name string, mf io.Reader, fn func(status string)) error {
fn("looking for model") fn("looking for model")
mf, err := GetManifest(ParseModelPath(c.Arg)) mf, err := GetManifest(ParseModelPath(c.Arg))
if err != nil { if err != nil {
// if we couldn't read the manifest, try getting the bin file fp := c.Arg
fp, err := getAbsPath(c.Arg)
if err != nil { // If filePath starts with ~/, replace it with the user's home directory.
fn("error determing path. exiting.") if strings.HasPrefix(fp, "~/") {
return err parts := strings.Split(fp, "/")
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("failed to open file: %v", err)
}
fp = filepath.Join(home, filepath.Join(parts[1:]...))
}
// If filePath is not an absolute path, make it relative to the modelfile path
if !filepath.IsAbs(fp) {
fp = filepath.Join(filepath.Dir(path), fp)
} }
fn("creating model layer") fn("creating model layer")

View file

@ -144,15 +144,6 @@ func create(c *gin.Context) {
return return
} }
// NOTE consider passing the entire Modelfile in the json instead of the path to it
file, err := os.Open(req.Path)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
defer file.Close()
ch := make(chan any) ch := make(chan any)
go func() { go func() {
defer close(ch) defer close(ch)
@ -162,7 +153,7 @@ func create(c *gin.Context) {
} }
} }
if err := CreateModel(req.Name, file, fn); err != nil { if err := CreateModel(req.Name, req.Path, fn); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return return
} }