11package com .javabaas .shell .commands ;
22
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
4+ import com .javabaas .javasdk .JBApp ;
5+ import com .javabaas .javasdk .JBException ;
6+ import com .javabaas .javasdk .callback .JBAppListCallback ;
37import com .javabaas .shell .common .CommandContext ;
4- import com .javabaas .shell .entity .JBApp ;
8+ import com .javabaas .shell .entity .JBSApp ;
59import com .javabaas .shell .util .PropertiesUtil ;
610import com .javabaas .shell .util .SignUtil ;
711import org .fusesource .jansi .Ansi ;
1620
1721import javax .annotation .Resource ;
1822import java .util .HashMap ;
23+ import java .util .List ;
1924import java .util .Map ;
25+ import java .util .UUID ;
2026
2127/**
2228 * Created by Codi on 15/9/22.
@@ -30,6 +36,8 @@ public class AppCommands implements CommandMarker {
3036 private CommandContext context ;
3137 @ Resource (name = "AdminRestTemplate" )
3238 private RestTemplate rest ;
39+ @ Resource (name = "MasterRestTemplate" )
40+ private RestTemplate masterRest ;
3341 @ Autowired
3442 private SignUtil signUtil ;
3543 @ Autowired
@@ -44,10 +52,18 @@ public boolean isAvailable() {
4452 public void list () {
4553 context .cancelDoubleCheck ();
4654 try {
47- JBApp [] result = rest .getForObject (properties .getHost () + "admin/app/" , JBApp [].class );
48- for (JBApp app : result ) {
49- System .out .println (app .getName ());
50- }
55+ List <JBApp > list = JBApp .list ();
56+ list .forEach (o -> System .out .println (o .getName ()));
57+ // JBApp.listInBackground(new JBAppListCallback() {
58+ // @Override
59+ // public void done(boolean success, List<JBApp> list, JBException e) {
60+ // if (success) {
61+ // list.forEach(o -> System.out.println(o.getName()));
62+ // } else {
63+ // System.out.println(e.getMessage());
64+ // }
65+ // }
66+ // });
5167 } catch (HttpClientErrorException e ) {
5268 System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a (e .getResponseBodyAsString ()).reset ());
5369 }
@@ -57,9 +73,9 @@ public void list() {
5773 public void add (@ CliOption (key = {"" }, mandatory = true ) final String name ) {
5874 context .cancelDoubleCheck ();
5975 try {
60- Map < String , Object > field = new HashMap <> ();
61- field . put ( "name" , name );
62- rest . postForObject ( properties . getHost () + "admin/ app/" , field , String . class );
76+ JBApp app = new JBApp ();
77+ app . setName ( name );
78+ app . save ( );
6379 System .out .println (Ansi .ansi ().fg (Ansi .Color .GREEN ).a ("App added." ).reset ());
6480 set (name );
6581 } catch (HttpClientErrorException e ) {
@@ -72,28 +88,30 @@ public void delete(@CliOption(key = {""}, mandatory = true) final String name) {
7288 context .cancelDoubleCheck ();
7389 //显示类信息
7490 try {
75- JBApp [] apps = rest .getForObject (properties .getHost () + "admin/app/" , JBApp [].class );
76- for (JBApp app : apps ) {
91+ List <JBApp > list = JBApp .list ();
92+ final boolean [] flag = {false };
93+ list .forEach (app -> {
7794 if (app .getName ().equals (name )) {
95+ flag [0 ] = true ;
7896 System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("Do you really want to delete? (Y/N)" ));
7997 context .setDoubleCheck (new DoubleCheckListener () {
8098 @ Override
8199 public void confirm () {
82- rest .delete (properties . getHost () + "admin/app/" + app . getId (), String . class );
100+ app .delete ();
83101 context .setCurrentApp (null );
84102 System .out .println (Ansi .ansi ().fg (Ansi .Color .GREEN ).a ("App deleted." ).reset ());
85103 }
86-
87104 @ Override
88105 public void cancel () {
89106
90107 }
91108 });
92- return ;
93109 }
94- }
110+ });
95111 //未找到应用
96- System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("App not found!" ).reset ());
112+ if (!flag [0 ]) {
113+ System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("App not found!" ).reset ());
114+ }
97115 } catch (HttpClientErrorException e ) {
98116 System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a (e .getResponseBodyAsString ()).reset ());
99117 }
@@ -107,17 +125,21 @@ public void set(@CliOption(key = {""}, mandatory = false, help = "app name") fin
107125 context .setCurrentApp (null );
108126 } else {
109127 try {
110- JBApp [] apps = rest .getForObject (properties .getHost () + "admin/app/" , JBApp [].class );
111- for (JBApp app : apps ) {
128+ List <JBApp > list = JBApp .list ();
129+ final boolean [] flag = {false };
130+ list .forEach (app -> {
112131 if (app .getName ().equals (name )) {
113- rest .getForObject (properties .getHost () + "admin/app/" + app .getId (), String .class );
114- System .out .println ("Set current app to " + Ansi .ansi ().fg (Ansi .Color .GREEN ).a (app .getName ()).reset ());
115- context .setCurrentApp (app );
132+ JBApp jbApp = JBApp .get (app .getId ());
133+ flag [0 ] = true ;
134+ System .out .println ("Set current app to " + Ansi .ansi ().fg (Ansi .Color .GREEN ).a (jbApp .getName ()).reset ());
135+ context .setCurrentApp (jbApp );
116136 return ;
117137 }
118- }
138+ });
119139 //未找到应用
120- System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("App not found!" ).reset ());
140+ if (!flag [0 ]) {
141+ System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("App not found!" ).reset ());
142+ }
121143 } catch (HttpClientErrorException e ) {
122144 System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a (e .getResponseBodyAsString ()).reset ());
123145 }
@@ -136,8 +158,8 @@ public void export() {
136158 public void appInfo () {
137159 context .cancelDoubleCheck ();
138160 JBApp app = context .getCurrentApp ();
139- String appInfo = rest . getForObject ( properties . getHost () + "admin/ app/" + app .getId (), String . class );
140- System .out .println (appInfo );
161+ JBApp jbApp = JBApp . get ( app .getId ());
162+ System .out .println (jbApp );
141163 }
142164
143165 @ CliCommand (value = "import" , help = "Import tha app." )
@@ -156,14 +178,34 @@ public void token() {
156178 context .cancelDoubleCheck ();
157179 //获取令牌
158180 String timestamp = signUtil .getTimestamp ();
181+ String nonce = UUID .randomUUID ().toString ().replace ("-" , "" );
159182 System .out .println ("Timestamp: " + timestamp );
160- System .out .println ("AdminSign: " + signUtil .getAdminSign (timestamp ));
183+ System .out .println ("Nonce: " + nonce );
184+ System .out .println ("AdminSign: " + signUtil .getAdminSign (timestamp , nonce ));
161185 if (context .getCurrentApp () != null ) {
162186 System .out .println ("AppId: " + signUtil .getAppId ());
163187 System .out .println ("Key: " + context .getCurrentApp ().getKey ());
164188 System .out .println ("MasterKey: " + context .getCurrentApp ().getMasterKey ());
165- System .out .println ("Sign: " + signUtil .getSign (timestamp ));
166- System .out .println ("MasterSign: " + signUtil .getMasterSign (timestamp ));
189+ System .out .println ("Sign: " + signUtil .getSign (timestamp , nonce ));
190+ System .out .println ("MasterSign: " + signUtil .getMasterSign (timestamp , nonce ));
191+ }
192+ }
193+
194+ @ CliCommand (value = "account" , help = "Set Account." )
195+ public void setAccount (@ CliOption (key = {"" }, mandatory = true , help = "Input" ) final String input ) throws JsonProcessingException {
196+ context .cancelDoubleCheck ();
197+ String [] inputs = input .split (" " );
198+ if (inputs .length < 2 ) {
199+ System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("No Account!" ).reset ());
200+ return ;
201+ }
202+ String type = inputs [0 ];
203+ String account = inputs [1 ];
204+ try {
205+ masterRest .put (properties .getHost () + "master/account/setAccount/" + type , account );
206+ System .out .println (Ansi .ansi ().fg (Ansi .Color .GREEN ).a ("Object updated." ).reset ());
207+ } catch (HttpClientErrorException e ) {
208+ System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a (e .getResponseBodyAsString ()).reset ());
167209 }
168210 }
169211
0 commit comments