From 07c87facca7819515fbfe61dbfbac2511c100870 Mon Sep 17 00:00:00 2001 From: Dipjyoti Metia Date: Sat, 24 Feb 2024 16:46:40 +1100 Subject: [PATCH 1/2] inital spec analyser --- cmd/cmd.go | 1 + go.mod | 26 ++++--- go.sum | 43 ++++++----- pkg/commands/{command.go => genai.go} | 4 +- pkg/commands/local.go | 50 ++++++++++++ pkg/engine/files/files.go | 32 +++++++- pkg/engine/prompt/prompt.go | 2 +- pkg/engine/utils/walker.go | 107 ++++++++++++++++++++++++++ 8 files changed, 233 insertions(+), 32 deletions(-) rename pkg/commands/{command.go => genai.go} (97%) create mode 100644 pkg/commands/local.go create mode 100644 pkg/engine/utils/walker.go diff --git a/cmd/cmd.go b/cmd/cmd.go index ab8a33b..f8db54a 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -27,6 +27,7 @@ func init() { rootCmd.PersistentFlags().StringP("author", "a", "Dipjyoti Metia", "") rootCmd.AddCommand(commands.GenerateTestModule()) rootCmd.AddCommand(commands.GenerateTestScenarios()) + rootCmd.AddCommand(commands.SpecAnalyzer()) } func Execute() { diff --git a/go.mod b/go.mod index b581c2a..2c34095 100644 --- a/go.mod +++ b/go.mod @@ -11,9 +11,12 @@ require ( github.com/google/generative-ai-go v0.8.0 github.com/google/go-github/v59 v59.0.0 github.com/manifoldco/promptui v0.9.0 + github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/cobra v1.8.0 golang.org/x/oauth2 v0.17.0 - google.golang.org/api v0.166.0 + google.golang.org/api v0.167.0 + google.golang.org/protobuf v1.32.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -32,23 +35,25 @@ require ( github.com/google/s2a-go v0.1.7 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/googleapis/gax-go/v2 v2.12.1 // indirect - github.com/imdario/mergo v0.3.16 // indirect + github.com/imdario/mergo v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect github.com/perimeterx/marshmallow v1.1.5 // indirect + github.com/rivo/uniseg v0.4.7 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/tidwall/gjson v1.16.0 // indirect + github.com/tidwall/gjson v1.17.1 // indirect github.com/tidwall/match v1.1.1 // indirect - github.com/tidwall/pretty v1.2.0 // indirect + github.com/tidwall/pretty v1.2.1 // indirect go.opencensus.io v0.24.0 // indirect - go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.48.0 // indirect - go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.48.0 // indirect - go.opentelemetry.io/otel v1.23.1 // indirect - go.opentelemetry.io/otel/metric v1.23.1 // indirect - go.opentelemetry.io/otel/trace v1.23.1 // indirect + go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect + go.opentelemetry.io/otel v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/trace v1.24.0 // indirect golang.org/x/crypto v0.19.0 // indirect golang.org/x/net v0.21.0 // indirect golang.org/x/sync v0.6.0 // indirect @@ -61,5 +66,6 @@ require ( google.golang.org/genproto/googleapis/api v0.0.0-20240221002015-b0ce06bbee7c // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c // indirect google.golang.org/grpc v1.62.0 // indirect - google.golang.org/protobuf v1.32.0 // indirect ) + +replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.16 diff --git a/go.sum b/go.sum index b21a40d..0005bb7 100644 --- a/go.sum +++ b/go.sum @@ -32,8 +32,6 @@ github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04= github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= -github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa h1:jQCWAUqqlij9Pgj2i/PB79y4KOPYVyFYdROxgaCwdTQ= -github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa/go.mod h1:x/1Gn8zydmfq8dk6e9PdstVsDgu9RuyIIJqAaF//0IM= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/ctreminiom/go-atlassian v1.5.1 h1:GYji33ywQyWvePNQzxfvgb02fg6fcSH1g59vDy3ApEw= github.com/ctreminiom/go-atlassian v1.5.1/go.mod h1:R1EaOjo33pSN9Y6xIJCEgQ68wgwYoFYAXifnaPlmyc8= @@ -46,8 +44,6 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A= -github.com/envoyproxy/protoc-gen-validate v1.0.4/go.mod h1:qys6tmnRsYrQqIhm2bvKZH4Blx/1gTIZ2UKVY1M+Yew= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= @@ -124,6 +120,11 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= +github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s= github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU= @@ -131,6 +132,9 @@ github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzL github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= @@ -145,12 +149,14 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg= github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U= +github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= -github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo= github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= @@ -158,16 +164,16 @@ github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= -go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.48.0 h1:P+/g8GpuJGYbOp2tAdKrIPUX9JO02q8Q0YNlHolpibA= -go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.48.0/go.mod h1:tIKj3DbO8N9Y2xo52og3irLsPI4GW02DSMtrVgNMgxg= -go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.48.0 h1:doUP+ExOpH3spVTLS0FcWGLnQrPct/hD/bCPbDRUEAU= -go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.48.0/go.mod h1:rdENBZMT2OE6Ne/KLwpiXudnAsbdrdBaqBvTN8M8BgA= -go.opentelemetry.io/otel v1.23.1 h1:Za4UzOqJYS+MUczKI320AtqZHZb7EqxO00jAHE0jmQY= -go.opentelemetry.io/otel v1.23.1/go.mod h1:Td0134eafDLcTS4y+zQ26GE8u3dEuRBiBCTUIRHaikA= -go.opentelemetry.io/otel/metric v1.23.1 h1:PQJmqJ9u2QaJLBOELl1cxIdPcpbwzbkjfEyelTl2rlo= -go.opentelemetry.io/otel/metric v1.23.1/go.mod h1:mpG2QPlAfnK8yNhNJAxDZruU9Y1/HubbC+KyH8FaCWI= -go.opentelemetry.io/otel/trace v1.23.1 h1:4LrmmEd8AU2rFvU1zegmvqW7+kWarxtNOPyeL6HmYY8= -go.opentelemetry.io/otel/trace v1.23.1/go.mod h1:4IpnpJFwr1mo/6HL8XIPJaE9y0+u1KcVmuW7dwFSVrI= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 h1:4Pp6oUg3+e/6M4C0A/3kJ2VYa++dsWVTtGgLVj5xtHg= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0/go.mod h1:Mjt1i1INqiaoZOMGR1RIUJN+i3ChKoFRqzrRQhlkbs0= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw= +go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= +go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= +go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= +go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco= +go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= +go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -233,8 +239,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/api v0.166.0 h1:6m4NUwrZYhAaVIHZWxaKjw1L1vNAjtMwORmKRyEEo24= -google.golang.org/api v0.166.0/go.mod h1:4FcBc686KFi7QI/U51/2GKKevfZMpM17sCdibqe/bSA= +google.golang.org/api v0.167.0 h1:CKHrQD1BLRii6xdkatBDXyKzM0mkawt2QP+H3LtPmSE= +google.golang.org/api v0.167.0/go.mod h1:4FcBc686KFi7QI/U51/2GKKevfZMpM17sCdibqe/bSA= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM= @@ -268,6 +274,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/pkg/commands/command.go b/pkg/commands/genai.go similarity index 97% rename from pkg/commands/command.go rename to pkg/commands/genai.go index 96c9b36..c7356d3 100644 --- a/pkg/commands/command.go +++ b/pkg/commands/genai.go @@ -50,7 +50,7 @@ func GenerateTestModule() *cobra.Command { } spec := prompt.SelectLanguage(specContent) - file, err := files.IdentifySpecTypes(specPath) + file, err := files.ListFiles(specPath) if err != nil { return fmt.Errorf("failed to identify spec types: %w", err) } @@ -103,7 +103,7 @@ func GenerateTestScenarios() *cobra.Command { return fmt.Errorf("failed to create Gemini engine: %w", err) } - file, err := files.IdentifySpecTypes(specPath) + file, err := files.ListFiles(specPath) if err != nil { return fmt.Errorf("failed to identify spec types: %w", err) } diff --git a/pkg/commands/local.go b/pkg/commands/local.go new file mode 100644 index 0000000..0892464 --- /dev/null +++ b/pkg/commands/local.go @@ -0,0 +1,50 @@ +package commands + +import ( + "github.com/dipjyotimetia/jarvis/pkg/engine/files" + "github.com/dipjyotimetia/jarvis/pkg/engine/prompt" + "github.com/dipjyotimetia/jarvis/pkg/engine/utils" + "github.com/spf13/cobra" +) + +func setSpecPathFlag(cmd *cobra.Command) { + cmd.Flags().StringP("path", "p", "", "spec path") +} + +func SpecAnalyzer() *cobra.Command { + cmd := &cobra.Command{ + Use: "spec-analyzer", + Aliases: []string{"spec"}, + Short: "proto-analyzer is for analyzing protobuf spec files", + Long: `proto-analyzer is for analyzing protobuf spec files`, + RunE: func(cmd *cobra.Command, args []string) error { + specPath, _ := cmd.Flags().GetString("path") + + specContent := prompt.PromptContent{ + ErrorMsg: "Please provide a valid spec.", + Label: "What spec would you like to use?", + ItemType: "spec", + } + spec := prompt.SelectLanguage(specContent) + + specs, err := files.ListFiles(specPath) + if err != nil { + return err + } + if len(specs) == 0 { + return nil + } + switch spec { + case "protobuf": + utils.ProtoAnalyzer(specs) + case "openapi": + utils.OpenApiAnalyzer(specs) + default: + return nil + } + return nil + }, + } + setSpecPathFlag(cmd) + return cmd +} diff --git a/pkg/engine/files/files.go b/pkg/engine/files/files.go index be65fb7..c39207a 100644 --- a/pkg/engine/files/files.go +++ b/pkg/engine/files/files.go @@ -2,16 +2,19 @@ package files import ( "bufio" + "encoding/json" "fmt" "io" "io/fs" "os" "path/filepath" + "regexp" + "strings" "github.com/google/generative-ai-go/genai" ) -func IdentifySpecTypes(dir string) ([]string, error) { +func ListFiles(dir string) ([]string, error) { paths := []string{} filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { if err != nil { @@ -27,6 +30,33 @@ func IdentifySpecTypes(dir string) ([]string, error) { return paths, nil } +func identifyFileType(data []byte) string { + // OpenAPI (JSON) Check + var openAPI interface{} + if err := json.Unmarshal(data, &openAPI); err == nil { + if _, ok := openAPI.(map[string]interface{})["openapi"]; ok { + return "OpenAPI (JSON)" + } + } + + // OpenAPI (YAML) Check + if strings.Contains(string(data), "openapi:") || strings.Contains(string(data), "swagger:") { + return "OpenAPI (YAML)" + } + + // Protobuf Check (Very basic - Needs hints about expected message types) + if regexp.MustCompile(`(?m)^(package|message|syntax)\s`).Match(data) { + return "Protobuf (Likely)" + } + + // Avro Check (Rudimentary) + if strings.Contains(string(data), "{\"type\": \"record\"") { + return "Avro (Likely)" + } + + return "Unknown" +} + func ReadFile(spec string) ([]genai.Text, error) { file, err := os.Open(spec) if err != nil { diff --git a/pkg/engine/prompt/prompt.go b/pkg/engine/prompt/prompt.go index 834f974..1465a86 100644 --- a/pkg/engine/prompt/prompt.go +++ b/pkg/engine/prompt/prompt.go @@ -40,7 +40,7 @@ func SelectLanguage(pc PromptContent) string { case "language": items = []string{"Go", "Python", "JavaScript", "java", "TypeScript"} case "spec": - items = []string{"avro", "protobuf", "swagger", "OpenAPI3"} + items = []string{"avro", "protobuf", "swagger", "openapi"} default: items = []string{} fmt.Println("Invalid prompt type") diff --git a/pkg/engine/utils/walker.go b/pkg/engine/utils/walker.go new file mode 100644 index 0000000..6ecab31 --- /dev/null +++ b/pkg/engine/utils/walker.go @@ -0,0 +1,107 @@ +package utils + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/olekukonko/tablewriter" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/descriptorpb" + "gopkg.in/yaml.v3" +) + +type OpenAPI struct { + Paths map[string]PathItem `json:"paths"` +} + +type Operation struct { + OperationID string `json:"operationId"` +} + +type PathItem struct { + Get *Operation `json:"get"` + Post *Operation `json:"post"` + Put *Operation `json:"put"` + Delete *Operation `json:"delete"` + Patch *Operation `json:"patch"` +} + +func OpenApiAnalyzer(specFiles []string) { + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"Method", "Path", "OperationID"}) + + for _, specFile := range specFiles { + data, err := os.ReadFile(specFile) + if err != nil { + panic(err) + } + + var openapi OpenAPI + if err := json.Unmarshal(data, &openapi); err == nil { + } else if err := yaml.Unmarshal(data, &openapi); err == nil { + } else { + panic("Unsupported OpenAPI file format") + } + + for path, pathItem := range openapi.Paths { + if pathItem.Get != nil { + table.Append([]string{"GET", path, pathItem.Get.OperationID}) + } + if pathItem.Post != nil { + table.Append([]string{"POST", path, pathItem.Post.OperationID}) + } + if pathItem.Put != nil { + table.Append([]string{"PUT", path, pathItem.Put.OperationID}) + } + if pathItem.Patch != nil { + table.Append([]string{"PATCH", path, pathItem.Patch.OperationID}) + } + if pathItem.Delete != nil { + table.Append([]string{"DELETE", path, pathItem.Delete.OperationID}) + } + } + } + table.Render() +} + +func ProtoAnalyzer(protoFiles []string) { + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"File", "Service", "Method", "Input Type", "Output Type", "Streaming"}) + + for _, protoFile := range protoFiles { + data, err := os.ReadFile(protoFile) + if err != nil { + fmt.Println("Error reading file:", err) + continue + } + + fds := &descriptorpb.FileDescriptorSet{} + if err := proto.Unmarshal(data, fds); err != nil { + fmt.Println("Error parsing Proto:", err) + continue + } + + for _, file := range fds.File { + for _, service := range file.Service { + for _, method := range service.Method { + streaming := "No" + if method.GetClientStreaming() || method.GetServerStreaming() { + streaming = "Yes" + } + + table.Append([]string{ + file.GetName(), + service.GetName(), + method.GetName(), + method.GetInputType(), + method.GetOutputType(), + streaming, + }) + } + } + } + } + + table.Render() +} From ffe2994535fe190419489561f4f80d97e486cfd1 Mon Sep 17 00:00:00 2001 From: Dipjyoti Metia Date: Sun, 25 Feb 2024 19:04:23 +1100 Subject: [PATCH 2/2] update docs --- .github/ISSUE_TEMPLATE/bug.yml | 56 +++++++++++++++++++++++++++++ .github/ISSUE_TEMPLATE/feature.yml | 26 ++++++++++++++ .github/PULL_REQUEST_TEMPLATE.md | 21 +++++++++++ .github/config.yml | 43 ++++++++++++++++++++++ .github/workflows/release_draft.yml | 24 +++++++++++++ 5 files changed, 170 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug.yml create mode 100644 .github/ISSUE_TEMPLATE/feature.yml create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .github/config.yml create mode 100644 .github/workflows/release_draft.yml diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml new file mode 100644 index 0000000..fc38d00 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug.yml @@ -0,0 +1,56 @@ +name: 🐛 Bug Report +description: something isn't working as expected 🤔. +body: + - type: markdown + attributes: + value: | + Before opening a bug report, please search for the behaviour in the existing issues. + + --- + + Thank you for taking the time to file a bug report. To address this bug as fast as possible, we need some information. + - type: input + id: os + attributes: + label: Operating system + description: "Which operating system do you use? Please provide the version as well." + placeholder: "macOS Big Sur 11.5.2" + validations: + required: true + - type: input + id: golang + attributes: + label: go Version + description: "Please provide the go version." + placeholder: "GO 1.22" + validations: + required: true + - type: dropdown + id: location + attributes: + label: Project Location + description: Where is the project located? + options: + - Local + - Remote + - Somewhere else (please specify in the description!) + validations: + required: true + - type: textarea + id: bug-description + attributes: + label: Bug description + description: What happened? + validations: + required: true + - type: textarea + id: steps + attributes: + label: Steps to reproduce + description: Which steps do we need to take to reproduce this error? + - type: textarea + id: logs + attributes: + label: Relevant log output + description: If applicable, provide relevant log output. No need for backticks here. + render: shell \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/feature.yml b/.github/ISSUE_TEMPLATE/feature.yml new file mode 100644 index 0000000..35f6772 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature.yml @@ -0,0 +1,26 @@ +name: 🚀 Feature Request +description: I have a suggestion (and may want to implement it 🙂)! +body: + - type: markdown + attributes: + value: | + Please fill out the sections below to help everyone identify and fix the bug + - type: textarea + id: description + attributes: + label: Describe your issue + placeholder: When I click here this happens + validations: + required: true + - type: checkboxes + id: checks + attributes: + label: Extra fields + options: + - label: I have used the search function to check if an issue already exists + required: true + - label: I'd like to work on this issue + - type: markdown + attributes: + value: | + Thanks for reporting this issue! We will get back to you as soon as possible. \ No newline at end of file diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..811e2be --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,21 @@ +# Description + +Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change. + +Fixes # (issue) + +## Type of change + +Please delete options that are not relevant. + +- [ ] Bug fix (non-breaking change which fixes an issue) +- [ ] New feature (non-breaking change which adds functionality) +- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) +- [ ] This change requires a documentation update + +## Checklist: + +- [ ] My code follows the style guidelines of this project +- [ ] I have added new tests required for this feature. +- [ ] I have made corresponding changes to the documentation, if required. +- [ ] My changes generate no new warnings diff --git a/.github/config.yml b/.github/config.yml new file mode 100644 index 0000000..9778a16 --- /dev/null +++ b/.github/config.yml @@ -0,0 +1,43 @@ +name-template: 'v$RESOLVED_VERSION' +tag-template: 'v$RESOLVED_VERSION' +categories: + - title: '🚀 Features' + labels: + - 'feature' + - 'enhancement' + - title: '🐛 Bug Fixes' + labels: + - 'fix' + - 'bugfix' + - 'bug' + - title: '✅ Tests' + label: 'test' + - title: '⚡ Performance' + label: 'performance' + - title: '📝 Documentation' + label: 'docs' + - title: '🔐 Security' + label: 'security' + - title: '🧰 Maintenance' + label: + - 'cicd' + - 'tech' + - title: '🤖 Dependencies' + label: 'dependencies' +change-template: '- $TITLE @$AUTHOR (#$NUMBER)' +change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks. +version-resolver: + major: + labels: + - 'major' + minor: + labels: + - 'minor' + patch: + labels: + - 'patch' + default: patch +template: | + ## Changes + + $CHANGES \ No newline at end of file diff --git a/.github/workflows/release_draft.yml b/.github/workflows/release_draft.yml new file mode 100644 index 0000000..2f20e27 --- /dev/null +++ b/.github/workflows/release_draft.yml @@ -0,0 +1,24 @@ +name: Release Drafter + +on: + push: + branches: + - main + pull_request: + types: [opened, reopened, synchronize] + +permissions: + contents: read + +jobs: + update_release_draft: + permissions: + contents: write + pull-requests: write + runs-on: ubuntu-latest + steps: + - uses: release-drafter/release-drafter@v6 + with: + config-name: config.yml + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file