Flow

当您使用 Flow Components 根据涉及多个组件的使用构建某些内容时,您的实现可能变得有点混乱。为了简化这些用例,我们添加了一个 ComponentFlow ,它可以将多个组件执行一起挂接,作为 “flow” 。

以下列表展示了流程及其在 shell 中的输出示例:

Unresolved include directive in modules/ROOT/pages/components/flow/index.adoc - include::../../../../../src/test/java/org/springframework/shell/docs/FlowComponentSnippets.java[]
{"version": 2, "width": 85, "height": 15, "timestamp": 1645645867, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
[1.590847, "o", "java -jar spring-shell-sample-commands.jar"]
[5.968022, "o", "\r\n"]
[8.099727, "o", "\u001b[?1h\u001b=\u001b[?2004h\u001b[33mmy-shell:>\u001b[0m"]
[11.261894, "o", "\u001b[1mflow showcase\u001b[0m"]
[12.6451, "o", "\r\r\n\u001b[?1l\u001b>\u001b[?1000l\u001b[?2004l"]
[12.721206, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[12.780227, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mField1\u001b[0m \u001b[34m[Default defaultField1Value]\u001b[0m\r"]
[14.703281, "o", "\u001b[?1l\u001b>\u001b[?12;25h\u001b[K"]
[14.711232, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mField1\u001b[0m \u001b[34mdefaultField1Value\u001b[0m\r\n"]
[14.713588, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[14.720745, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mField2\u001b[0m \r"]
[16.084492, "o", "\u001b[9Ch\r"]
[16.176942, "o", "\u001b[10Ci\r"]
[16.620009, "o", "\u001b[?1l\u001b>\u001b[?12;25h\u001b[K"]
[16.625711, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mField2\u001b[0m \u001b[34mhi\u001b[0m\r\n"]
[16.628041, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[16.633919, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mConfirmation1\u001b[0m \u001b[2m(Y/n)\u001b[0m\r"]
[20.090558, "o", "\u001b[?1l\u001b>\u001b[?12;25h\u001b[K"]
[20.098654, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mConfirmation1\u001b[0m \u001b[34mtrue\u001b[0m\r\n"]
[20.101548, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[20.105099, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mPath1\u001b[0m \r"]
[21.53213, "o", "\u001b[8Cp\u001b[134C \u001b[32m>\u001b[0m \u001b[32mPath ok\u001b[0m\u001b[A\r"]
[21.626297, "o", "\u001b[9Ca\r"]
[21.896442, "o", "\u001b[10Ct\r"]
[21.941151, "o", "\u001b[11Ch\r"]
[23.041717, "o", "\u001b[?1l\u001b>\u001b[?12;25h\u001b[K\r\r\n\u001b[K\u001b[A"]
[23.044506, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mPath1\u001b[0m \u001b[34mpath\u001b[0m\r\n"]
[23.049225, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[23.05459, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mSingle1\u001b[0m [Use arrows to move], type to filter\u001b[97C \u001b[96;1m>\u001b[0m\u001b[96;1m key1\u001b[0m\u001b[137C   key2\u001b[2A\r"]
[24.520415, "o", "\r\r\n  key1\r\r\n\u001b[96;1m> key2\u001b[0m\u001b[2A\r"]
[25.431855, "o", "\u001b[?1l\u001b>\u001b[?12;25h\u001b[K\r\r\n\u001b[K\r\r\n\u001b[K\u001b[2A"]
[25.438619, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mSingle1\u001b[0m \u001b[34mvalue2\u001b[0m\r\n"]
[25.441975, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[25.446547, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mMulti1\u001b[0m [Use arrows to move], type to filter\u001b[98C \u001b[96;1m>\u001b[0m\u001b[96;1m \u001b[39m[ ]\u001b[0m key1\u001b[133C   \u001b[1m[ ]\u001b[0m key2\u001b[133C   \u001b[1m[ ]\u001b[0m key3\u001b[3A\r"]
[26.698294, "o", "\r\r\n\u001b[2C\u001b[32m[x]\u001b[0m\u001b[A\r"]
[28.022787, "o", "\r\r\n  \r\r\n\u001b[96;1m> \u001b[0m\u001b[2A\r"]
[28.110824, "o", "\r\r\n\r\n\u001b[2C\u001b[32m[x]\u001b[0m\u001b[2A\r"]
[29.250629, "o", "\r\r\n\r\n\u001b[2C\u001b[1m[ ]\u001b[0m\u001b[2A\r"]
[30.360368, "o", "\u001b[?1l\u001b>\u001b[?12;25h\u001b[K\r\r\n\u001b[K\r\r\n\u001b[K\r\r\n\u001b[K\u001b[3A"]
[30.368273, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mMulti1\u001b[0m \u001b[34mvalue1\u001b[0m\r\n"]
[30.370638, "o", "\u001b[?1h\u001b=\u001b[?2004h\u001b[33mmy-shell:>\u001b[0m"]

组件的正常执行顺序与使用构建器定义的相同。你可以有条件地选择在一个流程中跳到何处,方法是使用 next 函数并返回目标 component id。如果此返回 ID 等于 null 或不存在,那么流程基本上就会在此处停止。

Unresolved include directive in modules/ROOT/pages/components/flow/index.adoc - include::../../../../../src/test/java/org/springframework/shell/docs/FlowComponentSnippets.java[]
{"version": 2, "width": 85, "height": 11, "timestamp": 1645645867, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
[1.590847, "o", "java -jar spring-shell-sample-commands.jar"]
[5.030349, "o", "\r\n"]
[7.108752, "o", "\u001b[?1h\u001b=\u001b[?2004h\u001b[33mmy-shell:>\u001b[0m"]
[7.604731, "o", "\u001b[31mf\u001b[0m"]
[7.754184, "o", "\u001b[31ml\u001b[0m"]
[7.98951, "o", "\u001b[31mo\u001b[0m"]
[8.405578, "o", "\u001b[31mw\u001b[0m"]
[8.763007, "o", "\u001b[31m \u001b[0m"]
[8.963478, "o", "\u001b[31mc\u001b[0m"]
[9.036017, "o", "\u001b[31mo\u001b[0m"]
[9.353476, "o", "\u001b[31mn\u001b[0m"]
[9.517639, "o", "\u001b[8D\u001b[1mflow conditional\u001b[0m \u001b[K"]
[10.036978, "o", "\r\r\n\u001b[?1l\u001b>\u001b[?1000l\u001b[?2004l"]
[10.115908, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[10.176519, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mSingle1\u001b[0m [Use arrows to move], type to filter\u001b[97C \u001b[96;1m>\u001b[0m\u001b[96;1m Field1\u001b[0m\u001b[135C   Field2\u001b[2A\r"]
[11.824714, "o", "\u001b[?1l\u001b>\u001b[?12;25h\u001b[K\r\r\n\u001b[K\r\r\n\u001b[K\u001b[2A"]
[11.835781, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mSingle1\u001b[0m \u001b[34mfield1\u001b[0m\r\n"]
[11.838786, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[11.844824, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mField1\u001b[0m \u001b[34m[Default defaultField1Value]\u001b[0m\r"]
[12.734384, "o", "\u001b[?1l\u001b>\u001b[?12;25h\u001b[K"]
[12.740754, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mField1\u001b[0m \u001b[34mdefaultField1Value\u001b[0m\r\n"]
[12.74362, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[13.547149, "o", "\u001b[?1h\u001b=\u001b[?2004h\u001b[33mmy-shell:>\u001b[0m"]
[14.091367, "o", "\u001b[1mflow conditional\u001b[0m"]
[14.419251, "o", "\r\r\n\u001b[?1l\u001b>\u001b[?1000l\u001b[?2004l"]
[14.424986, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[14.432116, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mSingle1\u001b[0m [Use arrows to move], type to filter\u001b[97C \u001b[96;1m>\u001b[0m\u001b[96;1m Field1\u001b[0m\u001b[135C   Field2\u001b[2A\r"]
[14.995304, "o", "\r\r\n  Field1\r\r\n\u001b[96;1m> Field2\u001b[0m\u001b[2A\r"]
[15.409104, "o", "\u001b[?1l\u001b>\u001b[?12;25h\u001b[K\r\r\n\u001b[K\r\r\n\u001b[K\u001b[2A"]
[15.416729, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mSingle1\u001b[0m \u001b[34mfield2\u001b[0m\r\n"]
[15.41833, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[15.424318, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mField2\u001b[0m \u001b[34m[Default defaultField2Value]\u001b[0m\r"]
[16.002129, "o", "\u001b[?1l\u001b>\u001b[?12;25h\u001b[K"]
[16.007474, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mField2\u001b[0m \u001b[34mdefaultField2Value\u001b[0m\r\n"]
[16.010197, "o", "\u001b[?1h\u001b=\u001b[?2004h\u001b[33mmy-shell:>\u001b[0m"]
[19.865275, "o", "\r\r\n\u001b[?1l\u001b>\u001b[?1000l"]
[19.865497, "o", "\u001b[?2004l"]
[19.868634, "o", "\u001b[31morg.jline.reader.EndOfFileException\u001b[0m\r\n\u001b[31mDetails of the error have been omitted. You can use the \u001b[1mstacktrace\u001b[22m command to print the full stacktrace.\u001b[0m\r\n"]
[19.870354, "o", "\u001b[?1h\u001b=\u001b[?2004h\u001b[33mmy-shell:>\u001b[0m"]

运行流的结果返回 ComponentFlowResult,你可以用它来执行进一步的操作。