Skip to content

Commit

Permalink
swagutil: Add helpers for go-swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Dec 1, 2019
1 parent 3ec9263 commit 165a660
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
92 changes: 92 additions & 0 deletions contrib/swagutil/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "swagutil",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.swagutil.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".swagutil" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".swagutil")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
70 changes: 70 additions & 0 deletions contrib/swagutil/cmd/sanitize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"io/ioutil"
"os"

"github.com/spf13/cobra"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"

"github.com/ory/x/cmdx"
)

func sanitizeIter(raw string) string {
result := raw
gjson.Parse(raw).ForEach(func(key, value gjson.Result) bool {
var err error
if !key.Exists() {
return true
}

switch value.Type {
case gjson.JSON:
result, err = sjson.SetRaw(result, key.String(), sanitizeIter(value.Raw))
cmdx.Must(err, "could not update path (%s - %s): %s", key.Raw, value.Raw, err)
case gjson.String:
switch key.String() {
case "x-go-package":
fallthrough
case "x-go-name":
result, err = sjson.Delete(result, key.String())
cmdx.Must(err, "could not delete path (%s - %s): %s", key.Raw, value.Raw, err)
}
}
return true
})
return result
}

// sanitizeCmd represents the sanitize command
var sanitizeCmd = &cobra.Command{
Use: "sanitize <file>",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
file, err := ioutil.ReadFile(args[0])
cmdx.Must(err, "Unable to read file: %s", err)

result := []byte(sanitizeIter(string(file)))

err = os.Remove(args[0])
cmdx.Must(err, "Unable to delete file: %s", err)

err = ioutil.WriteFile(args[0], result, 0766)
cmdx.Must(err, "Unable to write file: %s", err)
},
}

func init() {
rootCmd.AddCommand(sanitizeCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// sanitizeCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// sanitizeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
22 changes: 22 additions & 0 deletions contrib/swagutil/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main

import "github.com/ory/kratos/contrib/swagutil/cmd"

func main() {
cmd.Execute()
}

0 comments on commit 165a660

Please sign in to comment.