001package org.apache.maven.scm.provider.tfs.command;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.Iterator;
024
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFile;
027import org.apache.maven.scm.ScmFileSet;
028import org.apache.maven.scm.log.ScmLogger;
029import org.apache.maven.scm.provider.ScmProviderRepository;
030import org.apache.maven.scm.provider.tfs.command.consumer.ErrorStreamConsumer;
031import org.apache.maven.scm.provider.tfs.command.consumer.FileListConsumer;
032import org.codehaus.plexus.util.cli.CommandLineException;
033import org.codehaus.plexus.util.cli.CommandLineUtils;
034import org.codehaus.plexus.util.cli.Commandline;
035import org.codehaus.plexus.util.cli.StreamConsumer;
036import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer;
037
038/**
039 * 
040 */
041public class TfsCommand
042{
043
044    private ScmLogger logger;
045
046    private Commandline command;
047
048    public TfsCommand( String cmd, ScmProviderRepository r, ScmFileSet f, ScmLogger logger )
049    {
050        command = new Commandline();
051        command.setExecutable( "tf" );
052        if ( f != null )
053        {
054            command.setWorkingDirectory( f.getBasedir().getAbsolutePath() );
055        }
056        
057        command.createArg().setValue( cmd );
058        
059        if ( r.getUser() != null )
060        {
061            command.createArg().setValue( "-login:" + r.getUser() + "," + r.getPassword() );
062        }
063        this.logger = logger;
064    }
065
066    public void addArgument( ScmFileSet f )
067    {
068        info( "files: " + f.getBasedir().getAbsolutePath() );
069        Iterator<File> iter = f.getFileList().iterator();
070        while ( iter.hasNext() )
071        {
072            command.createArg().setValue( ( (File) iter.next() ).getPath() );
073        }
074    }
075
076    public void addArgument( String s )
077    {
078        command.createArg().setValue( s );
079    }
080
081    public int execute( StreamConsumer out, ErrorStreamConsumer err )
082        throws ScmException
083    {
084        info( "Command line - " + getCommandString() );
085        int status;
086        try
087        {
088            status = CommandLineUtils.executeCommandLine( command, out, err );
089        }
090        catch ( CommandLineException e )
091        {
092            throw new ScmException( "Error while executing TFS command line - " + getCommandString(), e );
093        }
094        info( "err - " + err.getOutput() );
095        if ( out instanceof StringStreamConsumer )
096        {
097            StringStreamConsumer sc = (StringStreamConsumer) out;
098            debug( sc.getOutput() );
099        }
100        if ( out instanceof FileListConsumer )
101        {
102            FileListConsumer f = (FileListConsumer) out;
103            for ( Iterator<ScmFile> i = f.getFiles().iterator(); i.hasNext(); )
104            {
105                ScmFile file = i.next();
106                debug( file.getPath() );
107            }
108        }
109
110        return status;
111    }
112
113    public String getCommandString()
114    {
115        return command.toString();
116    }
117    
118    public Commandline getCommandline()
119    {
120        return command;
121    }
122
123    private void info( String message )
124    {
125        if ( logger != null )
126        {
127            logger.info( message );
128        }
129    }
130
131    private void debug( String message )
132    {
133        if ( logger != null )
134        {
135            logger.debug( message );
136        }
137    }
138
139}