Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2009-2011 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.sonatype.gshell.commands.standard;
 
 
 import java.util.List;
Set a variable or property.

Author(s):
Jason Dillon
Since:
2.5
 
 @Command(name="set")
 public class SetCommand
     extends CommandActionSupport
 {
     enum Mode
     {
         VARIABLE,
         PROPERTY
     }
 
     @Option(name = "m", longName = "mode")
     private Mode mode = .;
 
     @Option(name = "v", longName = "verbose")
     private boolean verbose;

    

Since:
2.5.6
 
     @Option(name = "e", longName = "evaluate")
     private boolean evaluate;
 
     @Argument(index = 0)
     private String name;
 
     @Argument(index = 1)
     private List<Stringvalues;
 
     @Inject
     public SetCommand installCompleters(@Named("variable-name"final Completer c1) {
         assert c1 != null;
         setCompleters(c1null);
         return this;
     }
 
     public Object execute(final CommandContext contextthrows Exception {
         assert context != null;
         IO io = context.getIo();
         MessageSource messages = getMessages();
 
         if ( == null) {
             return displayList(context);
         }
         else if ( == null) {
             io.error(getMessages().format("error.missing-arg"messages.getMessage("command.argument.values.token")));
             return .;
         }
 
         String value = Strings.join(.toArray(), " ");
 
         if () {
             Object result = context.getShell().execute(value);
             if (result == null || result instanceof Result) {
                 io.error(messages.format("error.expression-did-not-return-a-value"value));
                 return .;
             }
             value = result.toString();
         }
 
        switch () {
            case :
                .info("Setting system property: {}={}"value);
                System.setProperty(value);
                break;
            case :
                Variables vars = context.getVariables();
                .info("Setting variable: {}={}"value);
                vars.set(value);
                break;
        }
        return .;
    }
    private Object displayList(final CommandContext contextthrows Exception {
        assert context != null;
        IO io = context.getIo();
        // NOTE: Using io.outputStream to display values to avoid any ANSI encoding or other translation.
        switch () {
            case : {
                Properties props = System.getProperties();
                for (Object o : props.keySet()) {
                    String name = (Stringo;
                    String value = props.getProperty(name);
                    io.streams.out.print(name);
                    io.streams.out.print("='");
                    io.streams.out.print(value);
                    io.streams.out.println("'");
                }
                break;
            }
            case : {
                Variables variables = context.getVariables();
                Iterator<Stringiter = variables.names();
                while (iter.hasNext()) {
                    String name = iter.next();
                    Object value = variables.get(name);
                    io.streams.out.print(name);
                    io.streams.out.print("='");
                    io.streams.out.print(value);
                    io.streams.out.flush();
                    io.streams.out.print("'");
                    // When --verbose include the class details of the values
                    if ( && value != null) {
                        io.streams.out.print(" (");
                        io.streams.out.print(value.getClass());
                        io.streams.out.print(")");
                    }
                    io.streams.out.println();
                }
                break;
            }
        }
        // Manually flush the stream, normally framework only flushes io.out
        io.streams.out.flush();
        return .;
    }
New to GrepCode? Check out our FAQ X