diff --git a/.gitignore b/.gitignore index f1c181e..fd2fff0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,59 @@ +# Created by .ignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +/.idea/ +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries +*.iml +convert_service +convert-service + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ +cmake-build-release/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +### Go template # Binaries for programs and plugins *.exe *.exe~ @@ -10,3 +66,9 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out + +/node_modules/ +/vendor/ +google/ +package.json +yarn.lock diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..b2c0a7e --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,292 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + digest = "1:320e7ead93de9fd2b0e59b50fd92a4d50c1f8ab455d96bc2eb083267453a9709" + name = "github.com/asaskevich/govalidator" + packages = ["."] + pruneopts = "UT" + revision = "ccb8e960c48f04d6935e72476ae4a51028f9e22f" + version = "v9" + +[[projects]] + digest = "1:b24d38b282bacf9791408a080f606370efa3d364e4b5fd9ba0f7b87786d3b679" + name = "github.com/codegangsta/cli" + packages = ["."] + pruneopts = "UT" + revision = "cfb38830724cc34fedffe9a2a29fb54fa9169cd1" + version = "v1.20.0" + +[[projects]] + digest = "1:07f4fafe81587b642166199f41c3edcca12929fcfcc0261b00be5dea2f968f44" + name = "github.com/golang/protobuf" + packages = [ + "proto", + "ptypes", + "ptypes/any", + "ptypes/duration", + "ptypes/struct", + "ptypes/timestamp", + ] + pruneopts = "UT" + revision = "6c65a5562fc06764971b7c5d05c76c75e84bdbf7" + version = "v1.3.2" + +[[projects]] + branch = "master" + digest = "1:abd0cd78d6d27a98080f2b2a7fd39bb2f3d3eef6aa20c720093129f04fb41d78" + name = "github.com/integration-system/gowsdl" + packages = ["soap"] + pruneopts = "UT" + revision = "368713356c71961354e09ab9b734740ebdd94784" + +[[projects]] + digest = "1:7a5875a61368e41b45f1e2732a1298cb7ff306840f94d941c7a55ff7c0f2d32f" + name = "github.com/integration-system/isp-lib" + packages = [ + "http", + "logger", + "utils", + ] + pruneopts = "UT" + revision = "9d583f86516f92e855d15de30ff0dd40bb2a2ab2" + version = "v1.5.1" + +[[projects]] + digest = "1:e4f630893d2bbf8c268eba14e7cc92ef8844ee259a45e21ea77cf7656544cb7e" + name = "github.com/json-iterator/go" + packages = [ + ".", + "extra", + ] + pruneopts = "UT" + revision = "0ff49de124c6f76f8494e194af75bde0f1a49a29" + version = "v1.1.6" + +[[projects]] + digest = "1:823dc0f8e4cdad8d91e6aba4cdd985a035596a8aa02e9d2abf1bec347894c41f" + name = "github.com/klauspost/compress" + packages = [ + "flate", + "gzip", + "zlib", + ] + pruneopts = "UT" + revision = "23256797d1e4ccac36fad828362cdfef5b3aeb90" + version = "v1.7.4" + +[[projects]] + digest = "1:923c4d7194b42e054b2eb8a6c62824ac55e23ececc1c7e48d4da69c971c55954" + name = "github.com/klauspost/cpuid" + packages = ["."] + pruneopts = "UT" + revision = "05a8198c0f5a27739aec358908d7e12c64ce6eb7" + version = "v1.2.1" + +[[projects]] + digest = "1:31e761d97c76151dde79e9d28964a812c46efc5baee4085b86f68f0c654450de" + name = "github.com/konsorten/go-windows-terminal-sequences" + packages = ["."] + pruneopts = "UT" + revision = "f55edac94c9bbba5d6182a4be46d86a2c9b5b50e" + version = "v1.0.2" + +[[projects]] + digest = "1:7c084e0e780596dd2a7e20d25803909a9a43689c153de953520dfbc0b0e51166" + name = "github.com/mattn/go-colorable" + packages = ["."] + pruneopts = "UT" + revision = "8029fb3788e5a4a9c00e415f586a6d033f5d38b3" + version = "v0.1.2" + +[[projects]] + digest = "1:9b90c7639a41697f3d4ad12d7d67dfacc9a7a4a6e0bbfae4fc72d0da57c28871" + name = "github.com/mattn/go-isatty" + packages = ["."] + pruneopts = "UT" + revision = "1311e847b0cb909da63b5fecfb5370aa66236465" + version = "v0.0.8" + +[[projects]] + digest = "1:0356f3312c9bd1cbeda81505b7fd437501d8e778ab66998ef69f00d7f9b3a0d7" + name = "github.com/mattn/go-runewidth" + packages = ["."] + pruneopts = "UT" + revision = "3ee7d812e62a0804a7d0a324e0249ca2db3476d3" + version = "v0.0.4" + +[[projects]] + branch = "master" + digest = "1:2b32af4d2a529083275afc192d1067d8126b578c7a9613b26600e4df9c735155" + name = "github.com/mgutz/ansi" + packages = ["."] + pruneopts = "UT" + revision = "9520e82c474b0a04dd04f8a40959027271bab992" + +[[projects]] + digest = "1:33422d238f147d247752996a26574ac48dcf472976eda7f5134015f06bf16563" + name = "github.com/modern-go/concurrent" + packages = ["."] + pruneopts = "UT" + revision = "bacd9c7ef1dd9b15be4a9909b8ac7a4e313eec94" + version = "1.0.3" + +[[projects]] + digest = "1:e32bdbdb7c377a07a9a46378290059822efdce5c8d96fe71940d87cb4f918855" + name = "github.com/modern-go/reflect2" + packages = ["."] + pruneopts = "UT" + revision = "4b7aa43c6742a2c18fdef89dd197aaae7dac7ccd" + version = "1.0.1" + +[[projects]] + digest = "1:abcdbf03ca6ca13d3697e2186edc1f33863bbdac2b3a44dfa39015e8903f7409" + name = "github.com/olekukonko/tablewriter" + packages = ["."] + pruneopts = "UT" + revision = "e6d60cf7ba1f42d86d54cdf5508611c4aafb3970" + version = "v0.0.1" + +[[projects]] + digest = "1:04457f9f6f3ffc5fea48e71d62f2ca256637dee0a04d710288e27e05c8b41976" + name = "github.com/sirupsen/logrus" + packages = ["."] + pruneopts = "UT" + revision = "839c75faf7f98a33d445d181f3018b5c3409a45e" + version = "v1.4.2" + +[[projects]] + digest = "1:46aea6ffe39c3d95c13640c2515236ed3c5cdffc3c78c6c0ed4edec2caf7a0dc" + name = "github.com/tidwall/gjson" + packages = ["."] + pruneopts = "UT" + revision = "c5e72cdf74dff23857243dd662c465b810891c21" + version = "v1.3.2" + +[[projects]] + digest = "1:8453ddbed197809ee8ca28b06bd04e127bec9912deb4ba451fea7a1eca578328" + name = "github.com/tidwall/match" + packages = ["."] + pruneopts = "UT" + revision = "33827db735fff6510490d69a8622612558a557ed" + version = "v1.0.1" + +[[projects]] + digest = "1:ddfe0a54e5f9b29536a6d7b2defa376f2cb2b6e4234d676d7ff214d5b097cb50" + name = "github.com/tidwall/pretty" + packages = ["."] + pruneopts = "UT" + revision = "1166b9ac2b65e46a43d8618d30d1554f4652d49b" + version = "v1.0.0" + +[[projects]] + digest = "1:b70c951ba6fdeecfbd50dabe95aa5e1b973866ae9abbece46ad60348112214f2" + name = "github.com/tidwall/sjson" + packages = ["."] + pruneopts = "UT" + revision = "25fb082a20e29e83fb7b7ef5f5919166aad1f084" + version = "v1.0.4" + +[[projects]] + digest = "1:c468422f334a6b46a19448ad59aaffdfc0a36b08fdcc1c749a0b29b6453d7e59" + name = "github.com/valyala/bytebufferpool" + packages = ["."] + pruneopts = "UT" + revision = "e746df99fe4a3986f4d4f79e13c1e0117ce9c2f7" + version = "v1.0.0" + +[[projects]] + branch = "master" + digest = "1:9322129898822a4ca1922b129648a5385d1db6a9d61d1893ef5d2dd0c4423fae" + name = "github.com/valyala/fasthttp" + packages = [ + ".", + "fasthttputil", + "stackless", + ] + pruneopts = "UT" + revision = "ccaae97f5bb5886a037f5e8ca871d0e977a91df9" + +[[projects]] + digest = "1:39a425f98fb19427061a693fe6bf0683c9bddec4b25b17067e34fdb465e39cb9" + name = "github.com/x-cray/logrus-prefixed-formatter" + packages = ["."] + pruneopts = "UT" + revision = "bb2702d423886830dee131692131d35648c382e2" + version = "v0.5.2" + +[[projects]] + branch = "master" + digest = "1:bbe51412d9915d64ffaa96b51d409e070665efc5194fcf145c4a27d4133107a4" + name = "golang.org/x/crypto" + packages = ["ssh/terminal"] + pruneopts = "UT" + revision = "4def268fd1a49955bfb3dda92fe3db4f924f2285" + +[[projects]] + branch = "master" + digest = "1:76ee51c3f468493aff39dbacc401e8831fbb765104cbf613b89bef01cf4bad70" + name = "golang.org/x/net" + packages = ["context"] + pruneopts = "UT" + revision = "da137c7871d730100384dbcf36e6f8fa493aef5b" + +[[projects]] + branch = "master" + digest = "1:31d44814b45607afa77c6e8ef20ea8bacc1d1053fe084440c874f311732e7ec3" + name = "golang.org/x/sys" + packages = [ + "unix", + "windows", + ] + pruneopts = "UT" + revision = "fae7ac547cb717d141c433a2a173315e216b64c4" + +[[projects]] + branch = "master" + digest = "1:a910255c3b706b4d8f2b185ed6b40c8f31b805111be240def2bdf2c62b87591b" + name = "google.golang.org/genproto" + packages = [ + "googleapis/rpc/errdetails", + "googleapis/rpc/status", + ] + pruneopts = "UT" + revision = "3bdd9d9f5532d75d09efb230bd767d265245cfe5" + +[[projects]] + digest = "1:caa836d207326da987daf8eb4404ef80806fb843056367d2c3c1636f57ede119" + name = "google.golang.org/grpc" + packages = [ + "codes", + "connectivity", + "grpclog", + "internal", + "metadata", + "stats", + "status", + ] + pruneopts = "UT" + revision = "1d89a3c832915b2314551c1d2a506874d62e53f7" + version = "v1.22.0" + +[[projects]] + digest = "1:4d2e5a73dc1500038e504a8d78b986630e3626dc027bc030ba5c75da257cdb96" + name = "gopkg.in/yaml.v2" + packages = ["."] + pruneopts = "UT" + revision = "51d6538a90f86fe93ac480b35f37b2be17fef232" + version = "v2.2.2" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + input-imports = [ + "github.com/codegangsta/cli", + "github.com/integration-system/isp-lib/http", + "github.com/olekukonko/tablewriter", + "github.com/tidwall/gjson", + "github.com/tidwall/sjson", + "gopkg.in/yaml.v2", + ] + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..a109e23 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,34 @@ +# Gopkg.toml example +# +# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true + + +[prune] + go-tests = true + unused-packages = true + +[[override]] + name = "github.com/valyala/fasthttp" + branch = "master" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0a04128 --- /dev/null +++ b/LICENSE @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/cfg/domain.go b/cfg/domain.go new file mode 100644 index 0000000..83962bd --- /dev/null +++ b/cfg/domain.go @@ -0,0 +1,22 @@ +package cfg + +import ( + "time" +) + +type getModuleByUuidAndNameRequest struct { + Name string + Uuid string +} + +type Config struct { + Id int64 + Name string + Description string + ModuleId int32 + Version int32 + Active bool + CreatedAt time.Time + UpdatedAt time.Time + Data map[string]interface{} +} diff --git a/cfg/method_name.go b/cfg/method_name.go new file mode 100644 index 0000000..a0f6031 --- /dev/null +++ b/cfg/method_name.go @@ -0,0 +1,7 @@ +package cfg + +const ( + getAvailableConfigs = "/api/config/module/get_modules_info" + getConfigByModuleName = "/api/config/config/get_config_by_instance_uuid_and_module_name" + createUpdateConfig = "/api/config/config/create_update_config" +) diff --git a/cfg/methods.go b/cfg/methods.go new file mode 100644 index 0000000..f986c1d --- /dev/null +++ b/cfg/methods.go @@ -0,0 +1,57 @@ +package cfg + +import ( + "github.com/integration-system/isp-lib/http" + "isp-config-service/domain" +) + +const ( + instanceIdHeader = "x-instance-identity" + scheme = "http://" +) + +func NewConfigClient(client http.RestClient) *configClient { + return &configClient{ + client: client, + headers: make(map[string]string), + } +} + +type configClient struct { + client http.RestClient + headers map[string]string + + gateHost string + instanceUuid string +} + +func (c *configClient) ReceiveConfig(gateHost, instanceUuid string) { + c.instanceUuid = instanceUuid + c.headers[instanceIdHeader] = instanceUuid + c.gateHost = scheme + gateHost +} + +func (c *configClient) GetAvailableConfigs() ([]domain.ModuleInfo, error) { + response := make([]domain.ModuleInfo, 0) + if err := c.client.Invoke("POST", c.gateHost+getAvailableConfigs, c.headers, nil, &response); err != nil { + return nil, err + } + return response, nil +} + +func (c *configClient) GetConfigByModuleName(name string) (*Config, error) { + request := &getModuleByUuidAndNameRequest{Name: name, Uuid: c.instanceUuid} + response := new(Config) + if err := c.client.Invoke("POST", c.gateHost+getConfigByModuleName, c.headers, request, response); err != nil { + return nil, err + } + return response, nil +} + +func (c *configClient) CreateUpdateConfig(request Config) (*Config, error) { + response := new(Config) + if err := c.client.Invoke("POST", c.gateHost+createUpdateConfig, c.headers, &request, response); err != nil { + return nil, err + } + return response, nil +} diff --git a/command/common.go b/command/common.go new file mode 100644 index 0000000..8bb89f7 --- /dev/null +++ b/command/common.go @@ -0,0 +1,110 @@ +package command + +import ( + "encoding/json" + "fmt" + "github.com/codegangsta/cli" + "github.com/integration-system/isp-lib/http" + "github.com/valyala/fasthttp" + "isp-ctl/cfg" + "isp-ctl/flag" + "isp-ctl/service" + "strings" +) + +func checkFlags(c *cli.Context) error { + var ( + uuid, host string + color bool + ) + colorFlag := strings.Split(flag.ColorName, ", ") + if c.GlobalBool(colorFlag[0]) != false { + color = true + } else { + color = c.GlobalBool(colorFlag[1]) + } + service.ColorService.Enable = color + + service.ColorService.Print(nil) + + hostFlag := strings.Split(flag.GateHostName, ", ") + if c.GlobalString(hostFlag[0]) != "" { + host = c.GlobalString(hostFlag[0]) + } else { + host = c.GlobalString(hostFlag[1]) + } + + uuidFlag := strings.Split(flag.InstanceUuidName, ", ") + if c.GlobalString(uuidFlag[0]) != "" { + uuid = c.GlobalString(uuidFlag[0]) + } else { + uuid = c.GlobalString(uuidFlag[1]) + } + + if uuid == "" || host == "" { + if configuration, err := service.YamlService.Parse(); err != nil { + return err + } else { + if uuid == "" { + uuid = configuration.InstanceUuid + } + if host == "" { + host = configuration.GateHost + } + } + } + service.ConfigService.ReceiveConfiguration(host, uuid) + return nil +} + +func printAnswer(data interface{}) { + if answer, err := json.MarshalIndent(data, "", " "); err != nil { + fmt.Println(err) + } else { + service.ColorService.Print(answer) + } +} + +func getModuleConfiguration(moduleName string) (*cfg.Config, []byte) { + if moduleName == "" { + fmt.Println("Need module name. Use 'info' for get module names") + return nil, nil + } + + moduleConfiguration, err := service.ConfigClient.GetConfigByModuleName(moduleName) + if err != nil { + if errorResponse, ok := err.(http.ErrorResponse); ok { + if errorResponse.StatusCode == fasthttp.StatusNotFound { + fmt.Printf("Module '%s' not found\n", moduleName) + } else { + fmt.Println(err) + } + } else { + fmt.Println(err) + } + return nil, nil + } + + if jsonObject, err := json.Marshal(moduleConfiguration.Data); err != nil { + fmt.Println(err) + return nil, nil + } else { + return moduleConfiguration, jsonObject + } +} + +func createUpdateConfig(stringToChange string, configuration *cfg.Config) { + newData := make(map[string]interface{}) + if stringToChange != "" { + if err := json.Unmarshal([]byte(stringToChange), &newData); err != nil { + fmt.Println(err) + return + } + } + configuration.Data = newData + if resp, err := service.ConfigClient.CreateUpdateConfig(*configuration); err != nil { + fmt.Println(err) + } else { + printAnswer(resp.Data) + } +} diff --git a/command/delete.go b/command/delete.go new file mode 100644 index 0000000..971fd42 --- /dev/null +++ b/command/delete.go @@ -0,0 +1,44 @@ +package command + +import ( + "fmt" + "github.com/codegangsta/cli" + "github.com/tidwall/sjson" +) + +func Delete() cli.Command { + return cli.Command{ + Name: "delete", + Usage: "delete configuration info by module_name", + Action: deleteComm.action, + } +} + +var deleteComm deleteCommand + +type deleteCommand struct{} + +func (d deleteCommand) action(c *cli.Context) { + if err := checkFlags(c); err != nil { + fmt.Println(err) + return + } + moduleName := c.Args().First() + pathObject := c.Args().Get(1) + + moduleConfiguration, jsonObject := getModuleConfiguration(moduleName) + if moduleConfiguration == nil { + return + } + + if pathObject == "" { + createUpdateConfig("", moduleConfiguration) + } else { + if stringToChange, err := sjson.Delete(string(jsonObject), pathObject); err != nil { + fmt.Println(err) + return + } else { + createUpdateConfig(stringToChange, moduleConfiguration) + } + } +} diff --git a/command/get.go b/command/get.go new file mode 100644 index 0000000..21bd482 --- /dev/null +++ b/command/get.go @@ -0,0 +1,54 @@ +package command + +import ( + "encoding/json" + "fmt" + "github.com/codegangsta/cli" + "github.com/tidwall/gjson" +) + +func Get() cli.Command { + return cli.Command{ + Name: "get", + Usage: "get configuration info by module_name", + Action: get.action, + } +} + +var get getCommand + +type getCommand struct{} + +func (g getCommand) action(c *cli.Context) { + if err := checkFlags(c); err != nil { + fmt.Println(err) + return + } + moduleName := c.Args().First() + pathObject := c.Args().Get(1) + + moduleConfiguration, jsonObject := getModuleConfiguration(moduleName) + if moduleConfiguration == nil { + return + } + + if pathObject == "" { + printAnswer(moduleConfiguration.Data) + } else { + g.checkObject(jsonObject, pathObject) + } +} + +func (g getCommand) checkObject(jsonObject []byte, depth string) { + jsonString := gjson.Get(string(jsonObject), depth) + if jsonString.Raw == "" { + fmt.Printf("Path '%s' not found\n", depth) + } else { + var data interface{} + if err := json.Unmarshal([]byte(jsonString.Raw), &data); err != nil { + fmt.Println(err) + } else { + printAnswer(data) + } + } +} diff --git a/command/info.go b/command/info.go new file mode 100644 index 0000000..3325271 --- /dev/null +++ b/command/info.go @@ -0,0 +1,61 @@ +package command + +import ( + "fmt" + "github.com/codegangsta/cli" + "github.com/olekukonko/tablewriter" + "isp-ctl/service" + "os" +) + +const ( + tableHeaderModuleName = "MODULE_NAME" + tableHeaderStatus = "STATUS" + tableHeaderAddresses = "ADDRESSES" + + tableNotConnectedStatus = "NOT_CONNECTED" + tableConnectedStatus = "CONNECTED" +) + +func Status() cli.Command { + return cli.Command{ + Name: "status", + Usage: "get available configs", + Action: status.action, + } +} + +var status statusCommand + +type statusCommand struct{} + +func (statusCommand) action(c *cli.Context) { + if err := checkFlags(c); err != nil { + fmt.Println(err) + return + } + + if arrayOfModules, err := service.ConfigClient.GetAvailableConfigs(); err != nil { + fmt.Println(err) + } else { + table := tablewriter.NewWriter(os.Stdout) + table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false}) + table.SetHeader([]string{tableHeaderModuleName, tableHeaderStatus, tableHeaderAddresses}) + for _, module := range arrayOfModules { + arrayOfAddress := "" + connection := tableNotConnectedStatus + for _, value := range module.Status { + if arrayOfAddress == "" { + arrayOfAddress = fmt.Sprintf("%s", value.Address.IP) + } else { + arrayOfAddress = fmt.Sprintf("%s, %s", arrayOfAddress, value.Address) + } + } + if arrayOfAddress != "" { + connection = tableConnectedStatus + } + table.Append([]string{module.Name, connection, arrayOfAddress}) + } + table.Render() + } +} diff --git a/command/set.go b/command/set.go new file mode 100644 index 0000000..f40398a --- /dev/null +++ b/command/set.go @@ -0,0 +1,105 @@ +package command + +import ( + "bufio" + "encoding/json" + "fmt" + "github.com/codegangsta/cli" + "github.com/tidwall/sjson" + "os" + "strings" +) + +func Set() cli.Command { + return cli.Command{ + Name: "set", + Usage: "set configuration info", + Action: set.action, + } +} + +var set setCommand + +type setCommand struct{} + +func (s setCommand) action(c *cli.Context) { + if err := checkFlags(c); err != nil { + fmt.Println(err) + return + } + + moduleName := c.Args().First() + pathObject := c.Args().Get(1) + changeObject := c.Args().Get(2) + + moduleConfiguration, jsonObject := getModuleConfiguration(moduleName) + if moduleConfiguration == nil { + return + } + + if changeObject == "" { + fmt.Print("Enter new value: ") + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + changeObject = scanner.Text() + } + + if changeObject == "" { + fmt.Println("Error. Expected argument") + return + } + + if pathObject == "" { + createUpdateConfig(changeObject, moduleConfiguration) + return + } else { + changeArgument := s.parseObject(changeObject) + if stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument); err != nil { + fmt.Println(err) + return + } else { + createUpdateConfig(stringToChange, moduleConfiguration) + return + } + } +} + +func (s setCommand) parseObject(argument string) interface{} { + argument = strings.Replace(argument, " ", "", -1) + tryParse := []byte(argument) + if tryParse[0] == '"' && tryParse[len(tryParse)-1] == '"' { + tryParse = tryParse[1 : len(tryParse)-1] + return string(tryParse) + } + + if argument == "null" { + return nil + } + + mapStringInterface := make(map[string]interface{}) + if err := json.Unmarshal(tryParse, &mapStringInterface); err == nil { + return mapStringInterface + } + + arrayOfObject := make([]interface{}, 0) + if err := json.Unmarshal(tryParse, &arrayOfObject); err == nil { + return arrayOfObject + } + + var integer int64 + if err := json.Unmarshal(tryParse, &integer); err == nil { + return integer + } + + var float float64 + if err := json.Unmarshal(tryParse, &float); err == nil { + return float + } + + var flag bool + if err := json.Unmarshal(tryParse, &flag); err == nil { + return flag + } + + return argument +} diff --git a/conf/local_config.go b/conf/local_config.go new file mode 100644 index 0000000..ef4dd12 --- /dev/null +++ b/conf/local_config.go @@ -0,0 +1,6 @@ +package conf + +type Configuration struct { + GateHost string `yaml:"gateHost"` + InstanceUuid string `yaml:"instanceUuid"` +} diff --git a/flag/common.go b/flag/common.go new file mode 100644 index 0000000..93aa5c5 --- /dev/null +++ b/flag/common.go @@ -0,0 +1,9 @@ +package flag + +import "github.com/codegangsta/cli" + +var ( + Host = cli.StringFlag{Name: GateHostName} + Uuid = cli.StringFlag{Name: InstanceUuidName} + Color = cli.BoolFlag{Name: ColorName} +) diff --git a/flag/name.go b/flag/name.go new file mode 100644 index 0000000..72f1575 --- /dev/null +++ b/flag/name.go @@ -0,0 +1,7 @@ +package flag + +const ( + GateHostName = "gateHost, g" + InstanceUuidName = "instanceUuid, u" + ColorName = "color, c" +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..d3da92a --- /dev/null +++ b/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "github.com/codegangsta/cli" + "isp-ctl/command" + "isp-ctl/flag" + "isp-ctl/service" + "log" + "os" +) + +func main() { + defineConfigurationPath() + initCommands() +} + +func initCommands() { + app := cli.NewApp() + app.Usage = "cfg updater" + app.UsageText = + ` ispctl status [flag...] + ispctl get [flag...] module_name [property_path] + ispctl set [flag...] module_name [property_path] [new_object] + ispctl delete [flag...] module_name [property_path]` + + app.Flags = []cli.Flag{ + flag.Host, + flag.Uuid, + flag.Color, + } + + app.Commands = []cli.Command{ + command.Status(), + command.Get(), + command.Set(), + command.Delete(), + } + + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } +} + +func defineConfigurationPath() { + if os.Getenv("APP_MODE") == "dev" { + service.SetConfigurationPath("./conf/config.yml") + } else { + service.SetConfigurationPath("/etc/ispctl/config.yml") + } +} diff --git a/service/color.go b/service/color.go new file mode 100644 index 0000000..8823d50 --- /dev/null +++ b/service/color.go @@ -0,0 +1,21 @@ +package service + +import ( + "fmt" + "github.com/tidwall/pretty" +) + +var ColorService colorService + +type colorService struct { + Enable bool +} + +func (c colorService) Print(json []byte) { + if c.Enable { + result := pretty.Color(json, nil) + fmt.Println(string(result)) + } else { + fmt.Println(string(json)) + } +} diff --git a/service/common.go b/service/common.go new file mode 100644 index 0000000..f446e1a --- /dev/null +++ b/service/common.go @@ -0,0 +1,7 @@ +package service + +var configPath string + +func SetConfigurationPath(path string) { + configPath = path +} diff --git a/service/config.go b/service/config.go new file mode 100644 index 0000000..ca29429 --- /dev/null +++ b/service/config.go @@ -0,0 +1,16 @@ +package service + +import ( + "github.com/integration-system/isp-lib/http" + "isp-ctl/cfg" +) + +var ConfigClient = cfg.NewConfigClient(http.NewJsonRestClient()) + +var ConfigService configService + +type configService struct{} + +func (configService) ReceiveConfiguration(host, uuid string) { + ConfigClient.ReceiveConfig(host, uuid) +} diff --git a/service/yaml.go b/service/yaml.go new file mode 100644 index 0000000..b28ac3b --- /dev/null +++ b/service/yaml.go @@ -0,0 +1,22 @@ +package service + +import ( + "gopkg.in/yaml.v2" + "io/ioutil" + "isp-ctl/conf" +) + +var YamlService yamlService + +type yamlService struct{} + +func (yamlService) Parse() (*conf.Configuration, error) { + resp := new(conf.Configuration) + reader, err := ioutil.ReadFile(configPath) + if err != nil { + return nil, err + } else { + err := yaml.Unmarshal(reader, resp) + return resp, err + } +}