github.com/kaisawind/go-swagger@v0.19.0/cmd/swagger/commands/generate/spec.go (about)

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package generate
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"strings"
    23  
    24  	"github.com/go-openapi/loads"
    25  	"github.com/go-openapi/spec"
    26  	"github.com/go-swagger/go-swagger/scan"
    27  	"github.com/jessevdk/go-flags"
    28  	"gopkg.in/yaml.v2"
    29  )
    30  
    31  // SpecFile command to generate a swagger spec from a go application
    32  type SpecFile struct {
    33  	BasePath    string         `long:"base-path" short:"b" description:"the base path to use" default:"."`
    34  	BuildTags   string         `long:"tags" short:"t" description:"build tags" default:""`
    35  	ScanModels  bool           `long:"scan-models" short:"m" description:"includes models that were annotated with 'swagger:model'"`
    36  	Compact     bool           `long:"compact" description:"when present, doesn't prettify the json"`
    37  	Output      flags.Filename `long:"output" short:"o" description:"the file to write to"`
    38  	Input       flags.Filename `long:"input" short:"i" description:"the file to use as input"`
    39  	Include     []string       `long:"include" short:"c" description:"include packages matching pattern"`
    40  	Exclude     []string       `long:"exclude" short:"x" description:"exclude packages matching pattern"`
    41  	IncludeTags []string       `long:"include-tag" short:"" description:"include routes having specified tags (can be specified many times)"`
    42  	ExcludeTags []string       `long:"exclude-tag" short:"" description:"exclude routes having specified tags (can be specified many times)"`
    43  }
    44  
    45  // Execute runs this command
    46  func (s *SpecFile) Execute(args []string) error {
    47  	input, err := loadSpec(string(s.Input))
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	var opts scan.Opts
    53  	opts.BasePath = s.BasePath
    54  	opts.Input = input
    55  	opts.ScanModels = s.ScanModels
    56  	opts.BuildTags = s.BuildTags
    57  	opts.Include = s.Include
    58  	opts.Exclude = s.Exclude
    59  	opts.IncludeTags = s.IncludeTags
    60  	opts.ExcludeTags = s.ExcludeTags
    61  	swspec, err := scan.Application(opts)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	return writeToFile(swspec, !s.Compact, string(s.Output))
    67  }
    68  
    69  func loadSpec(input string) (*spec.Swagger, error) {
    70  	if fi, err := os.Stat(input); err == nil {
    71  		if fi.IsDir() {
    72  			return nil, fmt.Errorf("expected %q to be a file not a directory", input)
    73  		}
    74  		sp, err := loads.Spec(input)
    75  		if err != nil {
    76  			return nil, err
    77  		}
    78  		return sp.Spec(), nil
    79  	}
    80  	return nil, nil
    81  }
    82  
    83  func writeToFile(swspec *spec.Swagger, pretty bool, output string) error {
    84  	var b []byte
    85  	var err error
    86  
    87  	if strings.HasSuffix(output, "yml") || strings.HasSuffix(output, "yaml") {
    88  		b, err = marshalToYAMLFormat(swspec)
    89  	} else {
    90  		b, err = marshalToJSONFormat(swspec, pretty)
    91  	}
    92  
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	if output == "" {
    98  		fmt.Println(string(b))
    99  		return nil
   100  	}
   101  	return ioutil.WriteFile(output, b, 0644)
   102  }
   103  
   104  func marshalToJSONFormat(swspec *spec.Swagger, pretty bool) ([]byte, error) {
   105  	if pretty {
   106  		return json.MarshalIndent(swspec, "", "  ")
   107  	}
   108  	return json.Marshal(swspec)
   109  }
   110  
   111  func marshalToYAMLFormat(swspec *spec.Swagger) ([]byte, error) {
   112  	b, err := json.Marshal(swspec)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	var jsonObj interface{}
   118  	if err := yaml.Unmarshal(b, &jsonObj); err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	return yaml.Marshal(jsonObj)
   123  }