diff --git a/go.mod b/go.mod index 1e50353..3f1a4d0 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ module github.com/tracebloc/cli go 1.26.0 require ( + github.com/AlecAivazis/survey/v2 v2.3.7 github.com/fatih/color v1.19.0 github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 github.com/schollz/progressbar/v3 v3.19.0 @@ -35,9 +36,11 @@ require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/moby/spdystream v0.5.1 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect diff --git a/go.sum b/go.sum index 13a149f..3eeb7b2 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,15 @@ +github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ= +github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w= 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/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM= github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI= +github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -35,12 +41,16 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 h1:JeSE6pjso5THxAzdVpqr6/geYxZytqFMBCOtn/ujyeo= github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674/go.mod h1:r4w70xmWCQKmi1ONH4KIaBptdivuRPyosB9RmPlGEwA= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -50,12 +60,16 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= 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.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= github.com/moby/spdystream v0.5.1 h1:9sNYeYZUcci9R6/w7KDaFWEWeV4LStVG78Mpyq/Zm/Y= @@ -91,6 +105,7 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= @@ -98,23 +113,48 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0= go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8= go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o= golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8= golang.org/x/oauth2 v0.34.0 h1:hqK/t4AKgbqWkdkcAeI8XLmbK+4m4G5YeQRrmiotGlw= golang.org/x/oauth2 v0.34.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo= golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.39.0 h1:RclSuaJf32jOqZz74CkPA9qFuVTX7vhLlpfj/IGWlqY= golang.org/x/term v0.39.0/go.mod h1:yxzUCTP/U+FzoxfdKmLaA0RV1WgE0VY7hXBwKtY/4ww= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE= golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8= golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI= golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +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= google.golang.org/protobuf v1.36.12-0.20260120151049-f2248ac996af h1:+5/Sw3GsDNlEmu7TfklWKPdQ0Ykja5VEmq2i817+jbI= google.golang.org/protobuf v1.36.12-0.20260120151049-f2248ac996af/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/cli/dataset.go b/internal/cli/dataset.go index 5ec33a6..bfb7b8b 100644 --- a/internal/cli/dataset.go +++ b/internal/cli/dataset.go @@ -83,7 +83,8 @@ func newDatasetPushCmd() *cobra.Command { numberOfKeypoints int // Operations flags. - dryRun bool + dryRun bool + noInput bool // Ingestor SA name override. Used as the ServiceAccountName // of the ephemeral stage Pod, so the Pod inherits whatever @@ -146,11 +147,24 @@ Exit codes: 8 jobs-manager rejected the submit (4xx/5xx other than auth) 9 ingestion Job exited non-zero, or completed with row-level failures the summary panel reports`, - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + var localPath string + if len(args) > 0 { + localPath = args[0] + } + // Guided mode: on a terminal (and unless --no-input), prompt + // for whatever's still missing. Off a TTY / with --no-input, + // prompter stays nil and runDatasetPush keeps flag-only + // behavior. + interactive := !noInput && isInteractiveTTY() + var pr prompter + if interactive { + pr = surveyPrompter{} + } return runDatasetPush(cmd.Context(), cmd.OutOrStdout(), cmd.ErrOrStderr(), runDatasetPushArgs{ - LocalPath: args[0], + LocalPath: localPath, Kubeconfig: kubeconfigPath, Context: contextOverride, Namespace: nsOverride, @@ -168,6 +182,9 @@ Exit codes: IdempotencyKey: idempotencyKey, ImageDigest: imageDigest, Printer: printerFor(cmd), + Interactive: interactive, + Prompter: pr, + CategorySet: cmd.Flags().Changed("category"), }) }, } @@ -209,6 +226,8 @@ Exit codes: cmd.Flags().BoolVar(&dryRun, "dry-run", false, "validate + discover + walk, but don't create any cluster resources") + cmd.Flags().BoolVar(&noInput, "no-input", false, + "disable interactive prompts; fail on missing required values (for CI/scripts)") cmd.Flags().StringVar(&ingestorSAName, "ingestor-sa", "", "override the ingestor ServiceAccount name (default: \"ingestor\"); "+ "set this if you customized ingestionAuthz.serviceAccountName in the parent client chart") @@ -250,6 +269,15 @@ type runDatasetPushArgs struct { // the RunE from the persistent --plain flag (see printerFor). Printer *ui.Printer + // Interactive guided mode (#28). When Interactive is true, + // runDatasetPush prompts (via Prompter) for any missing core inputs + // before validation. CategorySet records whether --category was + // passed explicitly (its non-empty default would otherwise look + // like a deliberate choice). Prompter is nil off a TTY / --no-input. + Interactive bool + Prompter prompter + CategorySet bool + // Phase 4 (#152) fields. See the flag declarations for the // per-knob rationale; all three are optional. Detach bool @@ -267,6 +295,20 @@ type runDatasetPushArgs struct { // a bad label-column or oversized dataset gets the diagnostic in // milliseconds without a kubeconfig round-trip. func runDatasetPush(ctx context.Context, out, errOut io.Writer, a runDatasetPushArgs) error { + // 0. Guided mode: prompt for any missing core inputs before + // validation. Flags already provided win; non-TTY / --no-input + // leaves Prompter nil and skips straight to the flag-only path. + if a.Interactive && a.Prompter != nil { + if err := runInteractive(a.Printer, a.Prompter, &a, a.CategorySet); err != nil { + return &exitError{code: 3, err: fmt.Errorf("interactive setup: %w", err)} + } + } + if a.LocalPath == "" { + return &exitError{code: 3, err: errors.New( + "local dataset path is required — pass it as an argument, or run " + + "on a terminal without --no-input for guided prompts")} + } + // 1. Validate the table name BEFORE anything else. It's both // the MySQL identifier and the /data/shared// PVC // subdirectory — an unsanitized traversal name (../../etc) diff --git a/internal/cli/interactive.go b/internal/cli/interactive.go new file mode 100644 index 0000000..95357a6 --- /dev/null +++ b/internal/cli/interactive.go @@ -0,0 +1,141 @@ +package cli + +import ( + "os" + + "github.com/AlecAivazis/survey/v2" + "golang.org/x/term" + + "github.com/tracebloc/cli/internal/push" + "github.com/tracebloc/cli/internal/ui" +) + +// promptCategories is the ordered list offered by the interactive +// category picker — the categories `dataset push` supports today (the +// same set runDatasetPush's category gate accepts). semantic_ / +// instance_segmentation are omitted until they're implemented. +var promptCategories = []string{ + "image_classification", + "object_detection", + "keypoint_detection", + "text_classification", + "masked_language_modeling", + "tabular_classification", + "tabular_regression", + "time_series_forecasting", + "time_to_event_prediction", +} + +// prompter is the narrow seam over the interactive library. Production +// uses surveyPrompter (a real terminal); tests inject a fake that +// returns scripted answers, so the prompt-mapping logic is unit- +// testable without a pseudo-terminal — the same trick kubernetes.Interface +// uses to let cluster code run against a fake clientset. +type prompter interface { + // Input asks for free text. def pre-fills the answer; validate, if + // non-nil, rejects bad input and re-prompts. + Input(label, help, def string, validate func(string) error) (string, error) + // Select asks the user to pick one of options; def is the + // pre-highlighted choice. + Select(label, help string, options []string, def string) (string, error) +} + +// surveyPrompter is the production prompter, backed by +// AlecAivazis/survey/v2 against the real terminal. +type surveyPrompter struct{} + +func (surveyPrompter) Input(label, help, def string, validate func(string) error) (string, error) { + var ans string + q := &survey.Input{Message: label, Help: help, Default: def} + var opts []survey.AskOpt + if validate != nil { + // survey hands the validator the raw answer as interface{}; + // for an Input that's always a string. + opts = append(opts, survey.WithValidator(func(v interface{}) error { + s, _ := v.(string) + return validate(s) + })) + } + if err := survey.AskOne(q, &ans, opts...); err != nil { + return "", err + } + return ans, nil +} + +func (surveyPrompter) Select(label, help string, options []string, def string) (string, error) { + var ans string + q := &survey.Select{Message: label, Help: help, Options: options, Default: def} + if err := survey.AskOne(q, &ans); err != nil { + return "", err + } + return ans, nil +} + +// isInteractiveTTY reports whether we can run a guided prompt flow: +// both stdin (we read answers) and stdout (we draw prompts) must be a +// real terminal. Piped input, redirected output, or CI all fail this +// and fall back to flag-only behavior. +func isInteractiveTTY() bool { + return term.IsTerminal(int(os.Stdin.Fd())) && term.IsTerminal(int(os.Stdout.Fd())) +} + +// runInteractive fills the gaps in a's core push fields by prompting, +// then returns. It only prompts for what's still missing, so flags the +// user already passed win. categorySet says whether --category was set +// explicitly (vs left at its non-empty default), which would otherwise +// hide "the user didn't actually choose a category." +// +// Mutates a through the pointer. PR-b adds category-specific prompts +// (target-size, schema, number-of-keypoints) + a confirm screen. +func runInteractive(p *ui.Printer, pr prompter, a *runDatasetPushArgs, categorySet bool) error { + p.PromptHeader("Let's set up your dataset push") + p.Hintf("Press Enter to accept a default; Ctrl-C to cancel.") + + if a.LocalPath == "" { + ans, err := pr.Input("Path to your dataset directory", "e.g. ./my-data", "", nil) + if err != nil { + return err + } + a.LocalPath = ans + } + + if !categorySet { + ans, err := pr.Select("Task category", "what kind of data this is", + promptCategories, a.Spec.Category) + if err != nil { + return err + } + a.Spec.Category = ans + } + + if a.Spec.Table == "" { + ans, err := pr.Input("Destination table name", + "MySQL identifier + PVC subdir; letters, digits, underscore only", "", + push.ValidateTableName) + if err != nil { + return err + } + a.Spec.Table = ans + } + + if a.Spec.Intent == "" { + ans, err := pr.Select("Intent", "which split this data is", + []string{"train", "test"}, "train") + if err != nil { + return err + } + a.Spec.Intent = ans + } + + // masked_language_modeling is self-supervised — no label column. + if a.Spec.LabelColumn == "" && a.Spec.Category != "masked_language_modeling" { + ans, err := pr.Input("Label column", + "the column in labels.csv that holds the label", "label", nil) + if err != nil { + return err + } + a.Spec.LabelColumn = ans + } + + return nil +} diff --git a/internal/cli/interactive_test.go b/internal/cli/interactive_test.go new file mode 100644 index 0000000..81deec9 --- /dev/null +++ b/internal/cli/interactive_test.go @@ -0,0 +1,129 @@ +package cli + +import ( + "bytes" + "testing" + + "github.com/tracebloc/cli/internal/push" + "github.com/tracebloc/cli/internal/ui" +) + +// fakePrompter is the test double for the prompter seam: it returns +// scripted answers keyed by prompt label and records the order of +// labels asked, so tests can assert WHICH fields were prompted and how +// answers map onto SpecArgs — with no real terminal involved. +type fakePrompter struct { + answers map[string]string + asked []string +} + +func (f *fakePrompter) answer(label, def string) string { + f.asked = append(f.asked, label) + if a, ok := f.answers[label]; ok { + return a + } + return def +} + +func (f *fakePrompter) Input(label, _ /*help*/, def string, validate func(string) error) (string, error) { + ans := f.answer(label, def) + if validate != nil { + if err := validate(ans); err != nil { + return "", err + } + } + return ans, nil +} + +func (f *fakePrompter) Select(label, _ /*help*/ string, _ []string, def string) (string, error) { + return f.answer(label, def), nil +} + +func discardPrinter() *ui.Printer { return ui.New(&bytes.Buffer{}) } + +// TestRunInteractive_FillsAllWhenEmpty: a bare invocation prompts for +// every core field and maps the answers onto SpecArgs. +func TestRunInteractive_FillsAllWhenEmpty(t *testing.T) { + f := &fakePrompter{answers: map[string]string{ + "Path to your dataset directory": "./data", + "Task category": "tabular_classification", + "Destination table name": "churn_train", + "Intent": "test", + "Label column": "churned", + }} + a := &runDatasetPushArgs{Spec: push.SpecArgs{Category: "image_classification"}} + + if err := runInteractive(discardPrinter(), f, a, false); err != nil { + t.Fatalf("runInteractive: %v", err) + } + if a.LocalPath != "./data" { + t.Errorf("LocalPath = %q, want ./data", a.LocalPath) + } + if a.Spec.Category != "tabular_classification" { + t.Errorf("Category = %q, want tabular_classification", a.Spec.Category) + } + if a.Spec.Table != "churn_train" { + t.Errorf("Table = %q, want churn_train", a.Spec.Table) + } + if a.Spec.Intent != "test" { + t.Errorf("Intent = %q, want test", a.Spec.Intent) + } + if a.Spec.LabelColumn != "churned" { + t.Errorf("LabelColumn = %q, want churned", a.Spec.LabelColumn) + } +} + +// TestRunInteractive_SkipsProvidedValues: flags already set (and an +// explicit --category) mean nothing is prompted. +func TestRunInteractive_SkipsProvidedValues(t *testing.T) { + f := &fakePrompter{answers: map[string]string{}} + a := &runDatasetPushArgs{ + LocalPath: "./data", + Spec: push.SpecArgs{ + Category: "image_classification", Table: "t", Intent: "train", LabelColumn: "label", + }, + } + if err := runInteractive(discardPrinter(), f, a, true /*categorySet*/); err != nil { + t.Fatalf("runInteractive: %v", err) + } + if len(f.asked) != 0 { + t.Errorf("expected no prompts, but asked: %v", f.asked) + } +} + +// TestRunInteractive_MLMSkipsLabel: masked_language_modeling has no +// label column, so it must not be prompted. +func TestRunInteractive_MLMSkipsLabel(t *testing.T) { + f := &fakePrompter{answers: map[string]string{ + "Destination table name": "mlm_train", + "Intent": "train", + }} + a := &runDatasetPushArgs{ + LocalPath: "./data", + Spec: push.SpecArgs{Category: "masked_language_modeling"}, + } + if err := runInteractive(discardPrinter(), f, a, true); err != nil { + t.Fatalf("runInteractive: %v", err) + } + for _, l := range f.asked { + if l == "Label column" { + t.Errorf("masked_language_modeling should not prompt for a label column") + } + } + if a.Spec.Table != "mlm_train" || a.Spec.Intent != "train" { + t.Errorf("table/intent not filled: %+v", a.Spec) + } +} + +// TestRunInteractive_RejectsBadTable: the table prompt runs +// push.ValidateTableName, so an unsafe name surfaces as an error. +func TestRunInteractive_RejectsBadTable(t *testing.T) { + f := &fakePrompter{answers: map[string]string{"Destination table name": "../bad"}} + a := &runDatasetPushArgs{ + LocalPath: "./data", + Spec: push.SpecArgs{Category: "image_classification", Intent: "train", LabelColumn: "label"}, + } + if err := runInteractive(discardPrinter(), f, a, true); err == nil { + t.Fatal("expected an error for an invalid table name, got nil") + } +}