From 165a660f277588ed572d7843354c207f72f1678d Mon Sep 17 00:00:00 2001 From: aeneasr Date: Sat, 23 Nov 2019 00:59:13 +0100 Subject: [PATCH] swagutil: Add helpers for go-swagger See https://github.com/go-swagger/go-swagger/issues/2119 --- contrib/swagutil/cmd/root.go | 92 ++++++++++++++++++++++++++++++++ contrib/swagutil/cmd/sanitize.go | 70 ++++++++++++++++++++++++ contrib/swagutil/main.go | 22 ++++++++ 3 files changed, 184 insertions(+) create mode 100644 contrib/swagutil/cmd/root.go create mode 100644 contrib/swagutil/cmd/sanitize.go create mode 100644 contrib/swagutil/main.go diff --git a/contrib/swagutil/cmd/root.go b/contrib/swagutil/cmd/root.go new file mode 100644 index 000000000000..ae9b395cc715 --- /dev/null +++ b/contrib/swagutil/cmd/root.go @@ -0,0 +1,92 @@ +/* +Copyright © 2019 NAME HERE + +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()) + } +} diff --git a/contrib/swagutil/cmd/sanitize.go b/contrib/swagutil/cmd/sanitize.go new file mode 100644 index 000000000000..02f473b6fdf4 --- /dev/null +++ b/contrib/swagutil/cmd/sanitize.go @@ -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 ", + 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") +} diff --git a/contrib/swagutil/main.go b/contrib/swagutil/main.go new file mode 100644 index 000000000000..86fd08c8b4f0 --- /dev/null +++ b/contrib/swagutil/main.go @@ -0,0 +1,22 @@ +/* +Copyright © 2019 NAME HERE + +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() +}