ronantakizawa/codereview-bench huggingface dataset:
Repository: ollama/ollama
File: scripts/tag_latest.sh (Shell)

Code before:
#!/bin/sh

set -eu

# We use 2 different image repositories to handle combining architecture images into multiarch manifest
# (The ROCm image is x86 only and is not a multiarch manifest)
# For developers, you can override the DOCKER_ORG to generate multiarch manifests
#  DOCKER_ORG=jdoe VERSION=0.1.30 PUSH=1 ./scripts/tag_latest.sh
DOCKER_ORG=${DOCKER_ORG:-"ollama"}
ARCH_IMAGE_REPO=${ARCH_IMAGE_REPO:-"${DOCKER_ORG}/release"}
FINAL_IMAGE_REPO=${FINAL_IMAGE_REPO:-"${DOCKER_ORG}/ollama"}

# Set PUSH to a non-empty string to trigger push instead of load
PUSH=${PUSH:-""}

echo "Assembling manifest and tagging latest"
docker manifest rm ${FINAL_IMAGE_REPO}:latest || true
docker manifest create ${FINAL_IMAGE_REPO}:latest \
    ${ARCH_IMAGE_REPO}:$VERSION-amd64 \
    ${ARCH_IMAGE_REPO}:$VERSION-arm64

docker pull ${ARCH_IMAGE_REPO}:$VERSION-rocm
docker tag ${ARCH_IMAGE_REPO}:$VERSION-rocm ${FINAL_IMAGE_REPO}:rocm

if [ -n "${PUSH}" ]; then
    echo "Pushing latest tags up..."
    docker manifest push ${FINAL_IMAGE_REPO}:latest
    docker push ${FINAL_IMAGE_REPO}:rocm
else
    echo "Not pushing ${FINAL_IMAGE_REPO}:latest and ${FINAL_IMAGE_REPO}:rocm"
fi




Review comment (suggestion):
```suggestion
RELEASE_IMAGE_REPO=${RELEASE_IMAGE_REPO:-"${DOCKER_ORG}/release"}
```

Code after:
#!/bin/sh

set -eu

# We use 2 different image repositories to handle combining architecture images into multiarch manifest
# (The ROCm image is x86 only and is not a multiarch manifest)
# For developers, you can override the DOCKER_ORG to generate multiarch manifests
#  DOCKER_ORG=jdoe VERSION=0.1.30 PUSH=1 ./scripts/tag_latest.sh
DOCKER_ORG=${DOCKER_ORG:-"ollama"}
RELEASE_IMAGE_REPO=${RELEASE_IMAGE_REPO:-"${DOCKER_ORG}/release"}
FINAL_IMAGE_REPO=${FINAL_IMAGE_REPO:-"${DOCKER_ORG}/ollama"}

# Set PUSH to a non-empty string to trigger push instead of load
PUSH=${PUSH:-""}

echo "Assembling manifest and tagging latest"
docker manifest rm ${FINAL_IMAGE_REPO}:latest || true
docker manifest create ${FINAL_IMAGE_REPO}:latest \
    ${RELEASE_IMAGE_REPO}:$VERSION-amd64 \
    ${RELEASE_IMAGE_REPO}:$VERSION-arm64

docker pull ${RELEASE_IMAGE_REPO}:$VERSION-rocm
docker tag ${RELEASE_IMAGE_REPO}:$VERSION-rocm ${FINAL_IMAGE_REPO}:rocm

if [ -n "${PUSH}" ]; then
    echo "Pushing latest tags up..."
    docker manifest push ${FINAL_IMAGE_REPO}:latest
    docker push ${FINAL_IMAGE_REPO}:rocm
else
    echo "Not pushing ${FINAL_IMAGE_REPO}:latest and ${FINAL_IMAGE_REPO}:rocm"
fi
Repository: ollama/ollama
File: llm/ggml.go (Go)

Code before:

func (kv KV) HeadCount() uint64 {
	return kv.u64(fmt.Sprintf("%s.attention.head_count", kv.Architecture()))
}

func (kv KV) HeadCountKV() uint64 {
	return kv.u64(fmt.Sprintf("%s.attention.head_count_kv", kv.Architecture()))
}

func (kv KV) GQA() uint64 {
	if headCountKV := kv.HeadCountKV(); headCountKV > 0 {
		return kv.HeadCount() / headCountKV
	}

	return 0
}

func (kv KV) EmbeddingLength() uint64 {
	return kv.u64(fmt.Sprintf("%s.embedding_length", kv.Architecture()))
}

func (kv KV) ContextLength() uint64 {
	return kv.u64(fmt.Sprintf("%s.context_length", kv.Architecture()))
}

func (kv KV) Graph(context, batch int) (int64, bool) {
	// inp_embd: f32 * n_batch * n_embd
	inputEmbd := 4 * kv.EmbeddingLength() * uint64(batch)

	switch kv.Architecture() {
	case "gemma":
		// output: f32 * n_vocab * n_batch
		tokens := kv["tokenizer.ggml.tokens"].([]any)
		resultOutput := 4 * uint64(len(tokens)) * uint64(batch)
		return int64(inputEmbd + resultOutput), true
	case "llama":
		// inp_pos: i32 n_embd
		inputPos := kv.EmbeddingLength() / 2
		// KQ_mask: f32 n_ctx * n_batch
		kqMask := uint64(4 * context * batch)
		// kq: f32 n_ctx * n_batch * n_head
		kq := kqMask * kv.HeadCount()
		return int64(4*inputEmbd + inputPos + kqMask + kq), true
	}

	return 0, false
}

type Tensor struct {
	Name   string `json:"name"`
	Kind   uint32 `json:"kind"`

Review comment (performance):
```suggestion
func (kv KV) GraphEstimate(context, batch int) (int64, bool) {
```

Nit: I usually like the short names but wonder if we'd want to be more specific here

Code after:

func (kv KV) HeadCount() uint64 {
	return kv.u64(fmt.Sprintf("%s.attention.head_count", kv.Architecture()))
}

func (kv KV) HeadCountKV() uint64 {
	if headCountKV := kv.u64(fmt.Sprintf("%s.attention.head_count_kv", kv.Architecture())); headCountKV > 0 {
		return headCountKV
	}

	return 1
}

func (kv KV) GQA() uint64 {
	return kv.HeadCount() / kv.HeadCountKV()
}

func (kv KV) EmbeddingLength() uint64 {
	return kv.u64(fmt.Sprintf("%s.embedding_length", kv.Architecture()))
}

func (kv KV) ContextLength() uint64 {
	return kv.u64(fmt.Sprintf("%s.context_length", kv.Architecture()))
}

type Tensor struct {
	Name   string `json:"name"`
	Kind   uint32 `json:"kind"`
	Offset uint64 `json:"-"`

	// Shape is the number of elements in each dimension
	Shape []uint64 `json:"shape"`

	io.WriterTo `json:"-"`
}

func (t Tensor) blockSize() uint64 {
	switch {
	case t.Kind < 2:
		return 1
	case t.Kind < 10:
		return 32
	default:
		return 256
	}
}

func (t Tensor) typeSize() uint64 {
	blockSize := t.blockSize()

	switch t.Kind {
Repository: ollama/ollama
File: cmd/cmd.go (Go)

Code before:

	if opts.Prompt != "" {
		fmt.Println()
		fmt.Println()
	}

	if !latest.Done {
		return nil
	}

	verbose, err := cmd.Flags().GetBool("verbose")
	if err != nil {
		return err
	}

	if verbose {
		latest.Summary()
	}

	ctx = context.WithValue(cmd.Context(), generateContextKey("context"), latest.Context)
	cmd.SetContext(ctx)

	return nil
}

func getHostIPAndPort() (string, string, error) {
	hostStr := strings.Trim(strings.TrimSpace(os.Getenv("OLLAMA_HOST")), "\"'")
	if hostStr == "" {
		hostStr = "127.0.0.1:11434"
	}

	host, port, err := net.SplitHostPort(hostStr)
	if err != nil {
		var netErr *net.AddrError
		if errors.As(err, &netErr) {
			if netErr.Err == "missing port in address" {
				host, port, err = net.SplitHostPort(hostStr + ":11434")
				if err != nil {
					return "", "", err
				}
			} else {
				return "", "", err
			}
		} else {
			return "", "", err
		}
	}

	if host == "" {
		host = "0.0.0.0"
	}

Review comment (refactor):
Can we make this an exposed function so we can use it elsewhere in the repo for consistency?

In particular, we should wire it up to https://github.com/ollama/ollama/blob/main/api/client.go#L62

It might require a tiny bit of abstraction to adjust 0.0.0.0 vs localhost logic for listen vs. connect as a client.

Code after:
	}

	if opts.MultiModal {
		opts.Prompt, opts.Images, err = extractFileData(opts.Prompt)
		if err != nil {
			return err
		}
	}

	request := api.GenerateRequest{
		Model:    opts.Model,
		Prompt:   opts.Prompt,
		Context:  generateContext,
		Images:   opts.Images,
		Format:   opts.Format,
		System:   opts.System,
		Template: opts.Template,
		Options:  opts.Options,
	}

	if err := client.Generate(ctx, &request, fn); err != nil {
		if errors.Is(err, context.Canceled) {
			return nil
		}
		return err
	}

	if opts.Prompt != "" {
		fmt.Println()
		fmt.Println()
	}

	if !latest.Done {
		return nil
	}

	verbose, err := cmd.Flags().GetBool("verbose")
	if err != nil {
		return err
	}

	if verbose {
		latest.Summary()
	}

	ctx = context.WithValue(cmd.Context(), generateContextKey("context"), latest.Context)
	cmd.SetContext(ctx)

	return nil
}
Repository: ollama/ollama
File: cmd/cmd.go (Go)

Code before:
	}

	host, port, err := net.SplitHostPort(hostStr)
	if err != nil {
		var netErr *net.AddrError
		if errors.As(err, &netErr) {
			if netErr.Err == "missing port in address" {
				host, port, err = net.SplitHostPort(hostStr + ":11434")
				if err != nil {
					return "", "", err
				}
			} else {
				return "", "", err
			}
		} else {
			return "", "", err
		}
	}

	if host == "" {
		host = "0.0.0.0"
	}

	if ip := net.ParseIP(host); ip == nil {
		return "", "", fmt.Errorf("invalid IP address specified in OLLAMA_HOST")
	}

	if portNum, err := strconv.ParseInt(port, 10, 32); err != nil || portNum > 65535 || portNum < 1 {
		return "", "", fmt.Errorf("invalid port specified in OLLAMA_HOST")
	}

	return host, port, nil
}

func RunServer(cmd *cobra.Command, _ []string) error {
	host, port, err := getHostIPAndPort()
	if err != nil {
		return err
	}

	if err := initializeKeypair(); err != nil {
		return err
	}

	ln, err := net.Listen("tcp", net.JoinHostPort(host, port))
	if err != nil {
		return err
	}

	return server.Serve(ln)
}

Review comment (suggestion):
Wont this error on `localhost` or other hostnames?  I would assume we'd want to consider those valid.

Code after:
		fmt.Println()
	}

	if !latest.Done {
		return nil
	}

	verbose, err := cmd.Flags().GetBool("verbose")
	if err != nil {
		return err
	}

	if verbose {
		latest.Summary()
	}

	ctx = context.WithValue(cmd.Context(), generateContextKey("context"), latest.Context)
	cmd.SetContext(ctx)

	return nil
}

func RunServer(cmd *cobra.Command, _ []string) error {
	// retrieve the OLLAMA_HOST environment variable
	ollamaHost, err := api.GetOllamaHost()
	if err != nil {
		return err
	}

	if err := initializeKeypair(); err != nil {
		return err
	}

	ln, err := net.Listen("tcp", net.JoinHostPort(ollamaHost.Host, ollamaHost.Port))
	if err != nil {
		return err
	}

	return server.Serve(ln)
}

func initializeKeypair() error {
	home, err := os.UserHomeDir()
	if err != nil {
		return err
	}

	privKeyPath := filepath.Join(home, ".ollama", "id_ed25519")
	pubKeyPath := filepath.Join(home, ".ollama", "id_ed25519.pub")

	_, err = os.Stat(privKeyPath)
Repository: ollama/ollama
File: api/client.go (Go)

Code before:
func ClientFromEnvironment() (*Client, error) {
	defaultPort := "11434"

	hostVar := os.Getenv("OLLAMA_HOST")
	hostVar = strings.Trim(strings.TrimSpace(hostVar), "\"'")

	scheme, hostport, ok := strings.Cut(hostVar, "://")
	switch {
	case !ok:
		scheme, hostport = "http", hostVar
	case scheme == "http":
		defaultPort = "80"
	case scheme == "https":
		defaultPort = "443"
	}

	// trim trailing slashes
	hostport = strings.TrimRight(hostport, "/")

	host, port, err := net.SplitHostPort(hostport)
	if err != nil {
		host, port = "127.0.0.1", defaultPort
		if ip := net.ParseIP(strings.Trim(hostport, "[]")); ip != nil {
			host = ip.String()
		} else if hostport != "" {
			host = hostport
		}
	}

	if portNum, err := strconv.ParseInt(port, 10, 32); err != nil || portNum > 65535 || portNum < 0 {
		return nil, ErrInvalidHostPort
	}

	return &Client{
		base: &url.URL{
			Scheme: scheme,
			Host:   net.JoinHostPort(host, port),
		},
		http: http.DefaultClient,
	}, nil
}

func (c *Client) GetHost() string {
	return c.base.Host
}

func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error {
	var reqBody io.Reader
	var data []byte
	var err error


Review comment (suggestion):


Not sure if this is what you intended but this trims space before quotes. More common would be trim quotes then space? e.g. `OLLAMA_HOST="127.0.0.1:11434 "` vs. `OLLAMA_HOST="127.0.0.1:11434"<space>`


Code after:
func ClientFromEnvironment() (*Client, error) {
	ollamaHost, err := GetOllamaHost()
	if err != nil {
		return nil, err
	}

	return &Client{
		base: &url.URL{
			Scheme: ollamaHost.Scheme,
			Host:   net.JoinHostPort(ollamaHost.Host, ollamaHost.Port),
		},
		http: http.DefaultClient,
	}, nil
}

type OllamaHost struct {
	Scheme string
	Host   string
	Port   string
}

func GetOllamaHost() (OllamaHost, error) {
	defaultPort := "11434"

	hostVar := os.Getenv("OLLAMA_HOST")
	hostVar = strings.TrimSpace(strings.Trim(strings.TrimSpace(hostVar), "\"'"))

	scheme, hostport, ok := strings.Cut(hostVar, "://")
	switch {
	case !ok:
		scheme, hostport = "http", hostVar
	case scheme == "http":
		defaultPort = "80"
	case scheme == "https":
		defaultPort = "443"
	}

	// trim trailing slashes
	hostport = strings.TrimRight(hostport, "/")

	host, port, err := net.SplitHostPort(hostport)
	if err != nil {
		host, port = "127.0.0.1", defaultPort
		if ip := net.ParseIP(strings.Trim(hostport, "[]")); ip != nil {
			host = ip.String()
		} else if hostport != "" {
			host = hostport
		}
	}

	if portNum, err := strconv.ParseInt(port, 10, 32); err != nil || portNum > 65535 || portNum < 0 {
Repository: ollama/ollama
File: cmd/cmd.go (Go)

Code before:
	}

	if modelfile {
		flagsSet++
		showType = "modelfile"
	}

	if parameters {
		flagsSet++
		showType = "parameters"
	}

	if system {
		flagsSet++
		showType = "system"
	}

	if template {
		flagsSet++
		showType = "template"
	}

	if flagsSet > 1 {
		return errors.New("only one of '--license', '--modelfile', '--parameters', '--system', or '--template' can be specified")
	} else if flagsSet == 0 {
		return errors.New("one of '--license', '--modelfile', '--parameters', '--system', or '--template' must be specified")
	}

	req := api.ShowRequest{Name: args[0]}
	resp, err := client.Show(cmd.Context(), &req)
	if err != nil {
		return err
	}

	switch showType {
	case "license":
		fmt.Println(resp.License)
	case "modelfile":
		fmt.Println(resp.Modelfile)
	case "parameters":
		fmt.Println(resp.Parameters)
	case "system":
		fmt.Println(resp.System)
	case "template":
		fmt.Println(resp.Template)
	}

	return nil
}

func CopyHandler(cmd *cobra.Command, args []string) error {

Review comment (suggestion):
Originally we left this guy as `m.Name` because people could be using an old version of the server. I don't remember what version that was now though.

Code after:
			return errFromUnknownKey(err)
		}

		return err
	}

	spinner.Stop()
	return nil
}

func ListHandler(cmd *cobra.Command, args []string) error {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

	models, err := client.List(cmd.Context())
	if err != nil {
		return err
	}

	var data [][]string

	for _, m := range models.Models {
		if len(args) == 0 || strings.HasPrefix(m.Name, args[0]) {
			data = append(data, []string{m.Name, m.Digest[:12], format.HumanBytes(m.Size), format.HumanTime(m.ModifiedAt, "Never")})
		}
	}

	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"NAME", "ID", "SIZE", "MODIFIED"})
	table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
	table.SetAlignment(tablewriter.ALIGN_LEFT)
	table.SetHeaderLine(false)
	table.SetBorder(false)
	table.SetNoWhiteSpace(true)
	table.SetTablePadding("\t")
	table.AppendBulk(data)
	table.Render()

	return nil
}

func DeleteHandler(cmd *cobra.Command, args []string) error {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

	for _, name := range args {
		req := api.DeleteRequest{Name: name}
Repository: ollama/ollama
File: auth/auth.go (Go)

Code before:
	"log/slog"
	"os"
	"path/filepath"
	"strings"

	"golang.org/x/crypto/ssh"
)

const defaultPrivateKey = "id_ed25519"

func keyPath() (string, error) {
	home, err := os.UserHomeDir()
	if err != nil {
		return "", err
	}

	return filepath.Join(home, ".ollama", defaultPrivateKey), nil
}

func GetPublicKey() (string, error) {
	keyPath, err := keyPath()
	if err != nil {
		return "", err
	}

	pubKey, err := os.ReadFile(keyPath + ".pub") // note: the .pub suffix is extremely important to not leak the private key
	if err != nil {
		return "", err
	}

	return strings.TrimSpace(string(pubKey)), nil
}

func NewNonce(r io.Reader, length int) (string, error) {
	nonce := make([]byte, length)
	if _, err := io.ReadFull(r, nonce); err != nil {
		return "", err
	}

	return base64.RawURLEncoding.EncodeToString(nonce), nil
}

func Sign(ctx context.Context, bts []byte) (string, error) {
	keyPath, err := keyPath()
	if err != nil {
		return "", err
	}

	privateKeyFile, err := os.ReadFile(keyPath)
	if err != nil {
		slog.Info(fmt.Sprintf("Failed to load private key: %v", err))

Review comment (security):
Can we parse the pubkey to _ensure_ that it is actually a public key? I think we can use `ssh.ParseAuthorizedKey([]byte(pubKey))` from `crypto/ssh`. That would make it so we _always_ return a pubkey instead of something else.

Code after:
	"log/slog"
	"os"
	"path/filepath"
	"strings"

	"golang.org/x/crypto/ssh"
)

const defaultPrivateKey = "id_ed25519"

func keyPath() (string, error) {
	home, err := os.UserHomeDir()
	if err != nil {
		return "", err
	}

	return filepath.Join(home, ".ollama", defaultPrivateKey), nil
}

func GetPublicKey() (string, error) {
	keyPath, err := keyPath()
	if err != nil {
		return "", err
	}

	privateKeyFile, err := os.ReadFile(keyPath)
	if err != nil {
		slog.Info(fmt.Sprintf("Failed to load private key: %v", err))
		return "", err
	}

	privateKey, err := ssh.ParsePrivateKey(privateKeyFile)
	if err != nil {
		return "", err
	}

	publicKey := ssh.MarshalAuthorizedKey(privateKey.PublicKey())

	return strings.TrimSpace(string(publicKey)), nil
}

func NewNonce(r io.Reader, length int) (string, error) {
	nonce := make([]byte, length)
	if _, err := io.ReadFull(r, nonce); err != nil {
		return "", err
	}

	return base64.RawURLEncoding.EncodeToString(nonce), nil
}

func Sign(ctx context.Context, bts []byte) (string, error) {
Repository: ollama/ollama
File: parser/parser.go (Go)

Code before:
package parser

import (
	"bufio"
	"bytes"
	"errors"
	"fmt"
	"io"
	"strings"
)

type Command struct {
	Name string
	Args string
	bytes.Buffer
}

func Parse(r io.Reader) ([]Command, error) {
	var cmds []Command
	var errs []error
	parseCommands(r)(func(cmd Command, err error) bool {
		if err != nil {
			errs = append(errs, err)
			return false
		}

		cmds = append(cmds, cmd)
		return true
	})

	if len(errs) > 0 {
		return nil, errs[0]
	}

	for _, cmd := range cmds {
		if cmd.Name == "model" {
			return cmds, nil
		}
	}

	return nil, errors.New("no FROM line")
}

const (
	stateName = iota
	stateArgs

Review comment (suggestion):
I personally think it would be easier to read this if parseCommands just returned an array of errors, rather than yielding.

Code after:
package parser

import (
	"bufio"
	"bytes"
	"errors"
	"fmt"
	"io"
	"log/slog"
	"slices"
)

type Command struct {
	Name string
	Args string
}

func (c *Command) Reset() {
	c.Name = ""
	c.Args = ""
}

func Parse(reader io.Reader) ([]Command, error) {
	var commands []Command
	var command, modelCommand Command

	scanner := bufio.NewScanner(reader)
	scanner.Buffer(make([]byte, 0, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize)
	scanner.Split(scanModelfile)
	for scanner.Scan() {
		line := scanner.Bytes()

		fields := bytes.SplitN(line, []byte(" "), 2)
		if len(fields) == 0 || len(fields[0]) == 0 {
			continue
		}

		switch string(bytes.ToUpper(fields[0])) {
		case "FROM":
			command.Name = "model"
			command.Args = string(bytes.TrimSpace(fields[1]))
			// copy command for validation
			modelCommand = command
		case "ADAPTER":
			command.Name = string(bytes.ToLower(fields[0]))
			command.Args = string(bytes.TrimSpace(fields[1]))
Repository: ollama/ollama
File: parser/parser.go (Go)

Code before:
			} else if newline(r) {
				return nil, fmt.Errorf("missing value for [%s]", b.String())
			} else {
				return nil, fmt.Errorf("unexpected rune %q for state %d", r, s)
			}
		case stateArgs:
			if r == '"' && b.Len() == 0 {
				quotes++
				s = stateMultiline
			} else if newline(r) {
				cmd.Args += b.String()
				b.Reset()

				cmds = append(cmds, cmd)
				cmd = Command{}
				s = stateName
			} else {
				if _, err := b.WriteRune(r); err != nil {
					return nil, err
				}
			}
		case stateMultiline:
			if r == '"' && b.Len() == 0 {
				quotes++
			} else if r == '"' {
				if quotes--; quotes == 0 {
					cmd.Args += b.String()
					b.Reset()

					cmds = append(cmds, cmd)
					cmd = Command{}
					s = stateName
				}
			} else {
				if _, err := b.WriteRune(r); err != nil {
					return nil, err
				}
			}
		case stateMessage:
			if space(r) && !isValidRole(b.String()) {
				return nil, errors.New("role must be one of \"system\", \"user\", or \"assistant\"")
			} else if space(r) {
				if _, err := b.WriteString(": "); err != nil {
					return nil, err
				}

				cmd.Args += b.String()
				b.Reset()
				s = stateArgs
			} else if newline(r) {
				return nil, fmt.Errorf("missing value for [%s]", b.String())

Review comment (suggestion):
I find this syntax really weird. Does it only subtract one from quotes if it's equal and then do the other stuff? This would be much easier to read as `if quotes-1 == 0 { quotes-- }`

Code after:
			if !slices.Contains([]string{"system", "user", "assistant"}, string(bytes.ToLower(fields[0]))) {
				return nil, fmt.Errorf("role must be one of \"system\", \"user\", or \"assistant\"")
			}
			command.Args = fmt.Sprintf("%s: %s", string(bytes.ToLower(fields[0])), string(fields[1]))
		default:
			if !bytes.HasPrefix(fields[0], []byte("#")) {
				// log a warning for unknown commands
				slog.Warn(fmt.Sprintf("Unknown command: %s", fields[0]))
			}
			continue
		}

		commands = append(commands, command)
		command.Reset()
	}

	if modelCommand.Args == "" {
		return nil, errors.New("no FROM line for the model was specified")
	}

	return commands, scanner.Err()
}

func scanModelfile(data []byte, atEOF bool) (advance int, token []byte, err error) {
	advance, token, err = scan([]byte(`"""`), []byte(`"""`), data, atEOF)
	if err != nil {
		return 0, nil, err
	}

	if advance > 0 && token != nil {
		return advance, token, nil
	}

	advance, token, err = scan([]byte(`"`), []byte(`"`), data, atEOF)
	if err != nil {
		return 0, nil, err
	}

	if advance > 0 && token != nil {
		return advance, token, nil
	}

	return bufio.ScanLines(data, atEOF)
}

func scan(openBytes, closeBytes, data []byte, atEOF bool) (advance int, token []byte, err error) {
	newline := bytes.IndexByte(data, '\n')

	if start := bytes.Index(data, openBytes); start >= 0 && start < newline {
		end := bytes.Index(data[start+len(openBytes):], closeBytes)
		if end < 0 {