github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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  	"os"
    21  	"strings"
    22  
    23  	"github.com/thetreep/go-swagger/codescan"
    24  
    25  	"github.com/go-openapi/loads"
    26  	"github.com/go-openapi/spec"
    27  	"github.com/jessevdk/go-flags"
    28  	"gopkg.in/yaml.v3"
    29  )
    30  
    31  // SpecFile command to generate a swagger spec from a go application
    32  type SpecFile struct {
    33  	WorkDir     string         `long:"work-dir" short:"w" 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:"an input swagger file with which to merge"`
    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  	ExcludeDeps bool           `long:"exclude-deps" short:"" description:"exclude all dependencies of project"`
    44  }
    45  
    46  // Execute runs this command
    47  func (s *SpecFile) Execute(args []string) error {
    48  	if len(args) == 0 { // by default consider all the paths under the working directory
    49  		args = []string{"./..."}
    50  	}
    51  
    52  	input, err := loadSpec(string(s.Input))
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	var opts codescan.Options
    58  	opts.Packages = args
    59  	opts.WorkDir = s.WorkDir
    60  	opts.InputSpec = input
    61  	opts.ScanModels = s.ScanModels
    62  	opts.BuildTags = s.BuildTags
    63  	opts.Include = s.Include
    64  	opts.Exclude = s.Exclude
    65  	opts.IncludeTags = s.IncludeTags
    66  	opts.ExcludeTags = s.ExcludeTags
    67  	opts.ExcludeDeps = s.ExcludeDeps
    68  	swspec, err := codescan.Run(&opts)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	return writeToFile(swspec, !s.Compact, string(s.Output))
    74  }
    75  
    76  func loadSpec(input string) (*spec.Swagger, error) {
    77  	if fi, err := os.Stat(input); err == nil {
    78  		if fi.IsDir() {
    79  			return nil, fmt.Errorf("expected %q to be a file not a directory", input)
    80  		}
    81  		sp, err := loads.Spec(input)
    82  		if err != nil {
    83  			return nil, err
    84  		}
    85  		return sp.Spec(), nil
    86  	}
    87  	return nil, nil
    88  }
    89  
    90  func writeToFile(swspec *spec.Swagger, pretty bool, output string) error {
    91  	var b []byte
    92  	var err error
    93  
    94  	if strings.HasSuffix(output, "yml") || strings.HasSuffix(output, "yaml") {
    95  		b, err = marshalToYAMLFormat(swspec)
    96  	} else {
    97  		b, err = marshalToJSONFormat(swspec, pretty)
    98  	}
    99  
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	if output == "" {
   105  		fmt.Println(string(b))
   106  		return nil
   107  	}
   108  	return os.WriteFile(output, b, 0644) // #nosec
   109  }
   110  
   111  func marshalToJSONFormat(swspec *spec.Swagger, pretty bool) ([]byte, error) {
   112  	if pretty {
   113  		return json.MarshalIndent(swspec, "", "  ")
   114  	}
   115  	return json.Marshal(swspec)
   116  }
   117  
   118  func marshalToYAMLFormat(swspec *spec.Swagger) ([]byte, error) {
   119  	b, err := json.Marshal(swspec)
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  
   124  	var jsonObj interface{}
   125  	if err := yaml.Unmarshal(b, &jsonObj); err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	return yaml.Marshal(jsonObj)
   130  }