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.io.File;
 import java.net.URL;

Read and execute commands from a file in the current shell.

Author(s):
Jason Dillon
Since:
2.5
 
 @Command(name="source")
 public class SourceCommand
     extends CommandActionSupport
 {
     @Argument(required = true)
     private String path;
 
     @Inject
     public SourceCommand installCompleters(final @Named("file-name"Completer c1) {
         assert c1 != null;
         setCompleters(c1null);
         return this;
     }
 
     public Object execute(final CommandContext contextthrows Exception {
         assert context != null;
         Shell shell = context.getShell();
 
         URL url;
         try {
             url = new URL();
         }
         catch (MalformedURLException e) {
             url = new File().toURI().toURL();
         }
 
         BufferedReader reader = openReader(url);
         try {
             String line;
             while ((line = reader.readLine()) != null) {
                 shell.execute(line);
             }
         }
         finally {
             Closer.close(reader);
         }
 
         return .;
     }
 
     private BufferedReader openReader(final Object sourcethrows IOException {
         BufferedReader reader;
 
         if (source instanceof File) {
             File file = (Filesource;
             .info("Using source file: {}"file);
 
             new FileAssert(file).exists().isFile().isReadable();
 
             reader = new BufferedReader(new FileReader(file));
         }
         else if (source instanceof URL) {
             URL url = (URLsource;
             .info("Using source URL: {}"url);
 
             reader = new BufferedReader(new InputStreamReader(url.openStream()));
         }
        else {
            String tmp = String.valueOf(source);
            // First try a URL
            try {
                URL url = new URL(tmp);
                .info("Using source URL: {}"url);
                reader = new BufferedReader(new InputStreamReader(url.openStream()));
            }
            catch (MalformedURLException e) {
                // They try a file
                File file = new File(tmp);
                .info("Using source file: {}"file);
                reader = new BufferedReader(new FileReader(tmp));
            }
        }
        return reader;
    }
New to GrepCode? Check out our FAQ X