From 917683e31720647e3ebfa5d90b8651a8dd76706d Mon Sep 17 00:00:00 2001 From: jakezhu9 Date: Sat, 23 Sep 2023 17:06:26 +0800 Subject: [PATCH 1/4] feat: add 'kpm import' to converts other formats to KCL file Signed-off-by: jakezhu9 --- go.mod | 11 ++++++ go.sum | 27 +++++++++++++ kpm.go | 1 + pkg/cmd/cmd_import.go | 92 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 pkg/cmd/cmd_import.go diff --git a/go.mod b/go.mod index be3ed45c..bf0bc37b 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,9 @@ require ( github.com/Microsoft/go-winio v0.6.0 // indirect github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect github.com/acomagu/bufpipe v1.0.4 // indirect + github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/beorn7/perks v1.0.1 // indirect + github.com/buger/jsonparser v1.1.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/chai2010/jsonv v1.1.3 // indirect github.com/chai2010/protorpc v1.1.4 // indirect @@ -34,22 +36,28 @@ require ( github.com/docker/go-metrics v0.0.1 // indirect github.com/docker/go-units v0.5.0 // indirect github.com/emirpasic/gods v1.18.1 // indirect + github.com/fatih/color v1.10.0 // indirect github.com/go-git/gcfg v1.5.0 // indirect github.com/go-git/go-billy/v5 v5.4.1 // indirect github.com/go-logr/logr v1.2.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect + github.com/goccy/go-yaml v1.11.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/golang/snappy v0.0.3 // indirect github.com/google/go-cmp v0.5.9 // indirect github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect github.com/gorilla/mux v1.8.0 // indirect + github.com/iancoleman/strcase v0.3.0 // indirect github.com/imdario/mergo v0.3.13 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/julienschmidt/httprouter v1.3.0 // indirect github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/klauspost/compress v1.16.0 // indirect + github.com/mailru/easyjson v0.7.7 // indirect + github.com/mattn/go-colorable v0.1.8 // indirect + github.com/mattn/go-isatty v0.0.12 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/moby/locker v1.0.1 // indirect @@ -63,8 +71,10 @@ require ( github.com/prometheus/client_model v0.3.0 // indirect github.com/prometheus/common v0.37.0 // indirect github.com/prometheus/procfs v0.8.0 // indirect + github.com/qri-io/jsonpointer v0.1.1 // indirect github.com/sergi/go-diff v1.1.0 // indirect github.com/skeema/knownhosts v1.1.0 // indirect + github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect go.opentelemetry.io/otel v1.14.0 // indirect go.opentelemetry.io/otel/trace v1.14.0 // indirect @@ -75,6 +85,7 @@ require ( golang.org/x/sys v0.7.0 // indirect golang.org/x/text v0.9.0 // indirect golang.org/x/tools v0.7.0 // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect google.golang.org/grpc v1.53.0 // indirect google.golang.org/protobuf v1.28.1 // indirect diff --git a/go.sum b/go.sum index b1856bd7..76c8c0ca 100644 --- a/go.sum +++ b/go.sum @@ -57,11 +57,15 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFI github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= +github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= +github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bshuster-repo/logrus-logstash-hook v1.0.0 h1:e+C0SB5R1pu//O4MQ3f9cFuPGoOVeF2fE4Og9otCc70= +github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= +github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd h1:rFt+Y/IK1aEZkEHchZRSq9OQbsSzIT/OrI8YFFmRIng= github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b h1:otBG+dV+YK+Soembjv71DPz3uX/V/6MMlSyD9JBQ6kQ= github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0 h1:nvj0OLI3YqYXer/kZD8Ri1aaunCxIEsOst1BVJswV0o= @@ -118,6 +122,8 @@ 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/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= +github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ= github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY= github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4= @@ -146,9 +152,14 @@ github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= +github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= +github.com/goccy/go-yaml v1.11.0 h1:n7Z+zx8S9f9KgzG6KtQKf+kwqXZlLNR2F6018Dgau54= +github.com/goccy/go-yaml v1.11.0/go.mod h1:H+mJrWtjPTJAHvRbV09MCK9xYwODM+wRTVFFTWckfng= github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -226,6 +237,8 @@ github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09 github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= +github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= +github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= @@ -234,6 +247,7 @@ github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7P github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -261,8 +275,15 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A= github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= +github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= @@ -339,6 +360,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo= github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4= +github.com/qri-io/jsonpointer v0.1.1 h1:prVZBZLL6TW5vsSB9fFHFAMBLI4b0ri5vribQlTJiBA= +github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH9v37ZaMV64= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -367,6 +390,8 @@ github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw= github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q= github.com/urfave/cli/v2 v2.25.0 h1:ykdZKuQey2zq0yin/l7JOm9Mh+pg72ngYMeB0ABn6q8= github.com/urfave/cli/v2 v2.25.0/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= +github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= +github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= @@ -518,6 +543,7 @@ golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -624,6 +650,7 @@ golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= diff --git a/kpm.go b/kpm.go index fc728b40..f6958d23 100644 --- a/kpm.go +++ b/kpm.go @@ -28,6 +28,7 @@ func main() { cmd.NewAddCmd(kpmcli), cmd.NewPkgCmd(kpmcli), cmd.NewMetadataCmd(kpmcli), + cmd.NewImportCmd(kpmcli), // todo: The following commands are bound to the oci registry. // Refactor them to compatible with the other registry. diff --git a/pkg/cmd/cmd_import.go b/pkg/cmd/cmd_import.go new file mode 100644 index 00000000..ee97a7d3 --- /dev/null +++ b/pkg/cmd/cmd_import.go @@ -0,0 +1,92 @@ +// Copyright 2023 The KCL Authors. All rights reserved. + +package cmd + +import ( + "github.com/urfave/cli/v2" + "kcl-lang.io/kcl-go/pkg/tools/gen" + "kcl-lang.io/kpm/pkg/client" + "kcl-lang.io/kpm/pkg/reporter" + "os" +) + +// NewImportCmd new a Command for `kpm import`. +func NewImportCmd(_ *client.KpmClient) *cli.Command { + return &cli.Command{ + Hidden: false, + Name: "import", + Usage: "convert other formats to KCL file", + Description: `import converts other formats to KCL file. + + Supported conversion modes: + json: convert JSON data to KCL data + yaml: convert YAML data to KCL data + gostruct: convert Go struct to KCL schema + jsonschema: convert JSON schema to KCL schema + terraformschema: convert Terraform schema to KCL schema + auto: automatically detect the input format`, + ArgsUsage: "", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "mode", + Aliases: []string{"m"}, + Usage: "mode of import", + DefaultText: "auto", + Value: "auto", + }, + &cli.StringFlag{ + Name: "output", + Aliases: []string{"o"}, + Usage: "output filename", + }, + &cli.BoolFlag{ + Name: "force", + Aliases: []string{"f"}, + Usage: "force overwrite output file", + }, + }, + Action: func(c *cli.Context) error { + if c.Args().Len() != 1 { + reporter.Report("kpm: invalid arguments") + reporter.ExitWithReport("kpm: run 'kpm import help' for more information.") + } + inputFile := c.Args().First() + + opt := &gen.GenKclOptions{} + switch c.String("mode") { + case "json": + opt.Mode = gen.ModeJson + case "yaml": + opt.Mode = gen.ModeYaml + case "gostruct": + opt.Mode = gen.ModeGoStruct + case "jsonschema": + opt.Mode = gen.ModeJsonSchema + case "terraformschema": + opt.Mode = gen.ModeTerraformSchema + case "auto": + opt.Mode = gen.ModeAuto + default: + reporter.Report("kpm: invalid mode: ", c.String("mode")) + reporter.ExitWithReport("kpm: run 'kpm import help' for more information.") + } + + outputFile := c.String("output") + if outputFile == "" { + outputFile = "generated.k" + reporter.Report("kpm: output file not specified, use default: ", outputFile) + } + + if _, err := os.Stat(outputFile); err == nil && !c.Bool("force") { + reporter.ExitWithReport("kpm: output file already exist, use --force to overwrite: ", outputFile) + } + + outputWriter, err := os.Create(outputFile) + if err != nil { + reporter.ExitWithReport("kpm: failed to create output file: ", outputFile) + } + + return gen.GenKcl(outputWriter, inputFile, nil, opt) + }, + } +} From f704c3220284848cb783a9c55283e2b7ba902804 Mon Sep 17 00:00:00 2001 From: jakezhu9 Date: Sat, 23 Sep 2023 17:49:54 +0800 Subject: [PATCH 2/4] docs: add docs for kpm import Signed-off-by: jakezhu9 --- docs/command-reference-zh/11.import.md | 60 ++++++++++++++++++++++++++ docs/command-reference/11.import.md | 60 ++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 docs/command-reference-zh/11.import.md create mode 100644 docs/command-reference/11.import.md diff --git a/docs/command-reference-zh/11.import.md b/docs/command-reference-zh/11.import.md new file mode 100644 index 00000000..33ebca04 --- /dev/null +++ b/docs/command-reference-zh/11.import.md @@ -0,0 +1,60 @@ +# kpm import + +将其它格式的文件转换为 kcl 文件。 + +## 使用 + +```shell +kpm import [options] +``` + +## 介绍 + +`kpm import` 会从指定的文件中读取内容,并将其转换为 kcl 文件。 + +## 选项 + +### --mode, -m + +指定转换模式,默认值为 `auto`,以下是支持的模式: + +- `json`: 将 json 数据转换为 kcl 数据。 +- `yaml`: 将 yaml 数据转换为 kcl 数据。 +- `gostruct`: 将 go 结构体转换为 kcl schema。 +- `jsonschema`: 将 json schema 转换为 kcl schema。 +- `terraformschema`: 将 terraform provider schema 转换为 kcl schema。关于如何获取 terraform provider schema 文件,请参考 [terraform schema](https://developer.hashicorp.com/terraform/cli/commands/providers/schema)。 +- `auto`: 自动检测文件类型,并使用对应的转换模式。 + +### --output, -o + +指定输出文件名,默认值为 `generated.k`。 + +### --force, -f + +强制覆盖输出文件。 + +### --help, -h + +展示 `kpm import` 命令的帮助信息。 + +## 示例 + +使用 `kpm import` 将 yaml 数据转换为 kcl 数据。 + +``` +$ cat < foo.yaml +kind: Service +name: kcl +EOF + +$ kpm import foo.yaml + +$ cat generated.k +""" +This file was generated by the KCL auto-gen tool. DO NOT EDIT. +Editing this file might prove futile when you re-run the KCL auto-gen generate command. +""" + +kind = "Service" +name = "kcl" +``` diff --git a/docs/command-reference/11.import.md b/docs/command-reference/11.import.md new file mode 100644 index 00000000..b751151b --- /dev/null +++ b/docs/command-reference/11.import.md @@ -0,0 +1,60 @@ +# kpm import + +Convert files in other formats to kcl files. + +## Usage + +```shell +kpm import [options] +``` + +## Description + +`kpm import` reads content from the specified file and converts it into a kcl file. + +## Options + +### --mode, -m + +Specify the conversion mode. Default value is `auto`. The following modes are supported: + +- `json`: Convert json data to kcl data. +- `yaml`: Convert yaml data to kcl data. +- `gostruct`: Convert go structures to kcl schema. +- `jsonschema`: Convert json schema to kcl schema. +- `terraformschema`: Convert terraform provider schema to kcl schema. For how to obtain terraform provider schema files, refer to [terraform schema](https://developer.hashicorp.com/terraform/cli/commands/providers/schema). +- `auto`: Automatically detect the file type and use the corresponding conversion mode. + +### --output, -o + +Specify the output file name. Default value is `generated.k`. + +### --force, -f + +Force overwrite of the output file. + +### --help, -h + +Display help information for the `kpm import` command. + +## Examples + +Use `kpm import` to convert yaml data to kcl data. + +``` +$ cat < foo.yaml +kind: Service +name: kcl +EOF + +$ kpm import foo.yaml + +$ cat generated.k +""" +This file was generated by the KCL auto-gen tool. DO NOT EDIT. +Editing this file might prove futile when you re-run the KCL auto-gen generate command. +""" + +kind = "Service" +name = "kcl" +``` \ No newline at end of file From 12a1a2a092104ece7c45a58f9ead24ddd79d4ae0 Mon Sep 17 00:00:00 2001 From: jakezhu9 Date: Mon, 25 Sep 2023 16:06:10 +0800 Subject: [PATCH 3/4] fix: e2e test Signed-off-by: jakezhu9 --- .../test_suites/kpm/exec_outside_pkg/help_msg/test_suite.stdout | 1 + 1 file changed, 1 insertion(+) diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/help_msg/test_suite.stdout b/test/e2e/test_suites/kpm/exec_outside_pkg/help_msg/test_suite.stdout index 177bc30d..5d96e56c 100644 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/help_msg/test_suite.stdout +++ b/test/e2e/test_suites/kpm/exec_outside_pkg/help_msg/test_suite.stdout @@ -3,6 +3,7 @@ COMMANDS: add add new dependency pkg package a kcl package into tar metadata output the resolved dependencies of a package + import convert other formats to KCL file run compile kcl package. login login to a registry logout logout from a registry From 2fe4d705524f80cdb12d26eaec23436a70708046 Mon Sep 17 00:00:00 2001 From: jakezhu9 Date: Sat, 7 Oct 2023 14:32:11 +0800 Subject: [PATCH 4/4] fix: return error with message Signed-off-by: jakezhu9 --- pkg/cmd/cmd_import.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pkg/cmd/cmd_import.go b/pkg/cmd/cmd_import.go index ee97a7d3..601ec956 100644 --- a/pkg/cmd/cmd_import.go +++ b/pkg/cmd/cmd_import.go @@ -3,6 +3,7 @@ package cmd import ( + "fmt" "github.com/urfave/cli/v2" "kcl-lang.io/kcl-go/pkg/tools/gen" "kcl-lang.io/kpm/pkg/client" @@ -11,7 +12,7 @@ import ( ) // NewImportCmd new a Command for `kpm import`. -func NewImportCmd(_ *client.KpmClient) *cli.Command { +func NewImportCmd(kpmcli *client.KpmClient) *cli.Command { return &cli.Command{ Hidden: false, Name: "import", @@ -47,8 +48,7 @@ func NewImportCmd(_ *client.KpmClient) *cli.Command { }, Action: func(c *cli.Context) error { if c.Args().Len() != 1 { - reporter.Report("kpm: invalid arguments") - reporter.ExitWithReport("kpm: run 'kpm import help' for more information.") + return fmt.Errorf("kpm: invalid arguments") } inputFile := c.Args().First() @@ -67,23 +67,22 @@ func NewImportCmd(_ *client.KpmClient) *cli.Command { case "auto": opt.Mode = gen.ModeAuto default: - reporter.Report("kpm: invalid mode: ", c.String("mode")) - reporter.ExitWithReport("kpm: run 'kpm import help' for more information.") + return fmt.Errorf("kpm: invalid mode: %s", c.String("mode")) } outputFile := c.String("output") if outputFile == "" { outputFile = "generated.k" - reporter.Report("kpm: output file not specified, use default: ", outputFile) + reporter.ReportMsgTo("kpm: output file not specified, use default: generated.k", kpmcli.GetLogWriter()) } if _, err := os.Stat(outputFile); err == nil && !c.Bool("force") { - reporter.ExitWithReport("kpm: output file already exist, use --force to overwrite: ", outputFile) + return fmt.Errorf("kpm: output file already exist, use --force to overwrite: %s", outputFile) } outputWriter, err := os.Create(outputFile) if err != nil { - reporter.ExitWithReport("kpm: failed to create output file: ", outputFile) + return fmt.Errorf("kpm: failed to create output file: %s", outputFile) } return gen.GenKcl(outputWriter, inputFile, nil, opt)