You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: expand.go
    path of file: ./repos/go-swagger/cmd/swagger/commands
    
    the code of the file until where you have to start completion: package commands

import (
	"encoding/json"
	"errors"
	"fmt"
	"log"
	"os"

	"github.com/go-openapi/loads"
	"github.com/go-openapi/spec"
	"github.com/go-openapi/swag"
	flags "github.com/jessevdk/go-flags"
)

// ExpandSpec is a command that expands the $refs in a swagger document.
//
// There are no specific options for this expansion.
type ExpandSpec struct {
	Compact bool           `long:"compact" description:"applies to JSON formatted specs. When present, doesn't prettify the json"`
	Output  flags.Filename `long:"output" short:"o" description:"the file to write to"`
    
func writeToFile(swspec *spec.Swagger, pretty bool, format string, output string) error {
	var b []byte
	var err error
	asJSON := format == "json"

	log.Println("format = ", format)
	switch {
	case pretty && asJSON:
		b, err = json.MarshalIndent(swspec, "", "  ")
	case asJSON:
		b, err = json.Marshal(swspec)
	default:
		// marshals as YAML
		b, err = json.Marshal(swspec)
		if err == nil {
			var data swag.JSONMapSlice
			if erg := json.Unmarshal(b, &data); erg != nil {
				log.Fatalln(erg)
			}
			var bb interface{}
			bb, err = data.MarshalYAML()
			if err == nil {
				b = bb.([]byte)
			}
		}
	}

	if err != nil {
		return err
	}

	if output == "" {
		fmt.Println(string(b))
		return nil
	}

	return os.WriteFile(output, b, 0644) // #nosec
}