|
| 1 | +/* |
| 2 | + * Copyright 2013-2024, Seqera Labs |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package nextflow.cli |
| 18 | +import com.beust.jcommander.Parameter |
| 19 | +import com.beust.jcommander.Parameters |
| 20 | +import groovy.transform.CompileStatic |
| 21 | +import groovy.util.logging.Slf4j |
| 22 | +import nextflow.exception.AbortOperationException |
| 23 | +import nextflow.plugin.Plugins |
| 24 | +import nextflow.scm.AssetManager |
| 25 | +import nextflow.scm.PushManager |
| 26 | +import nextflow.util.TestOnly |
| 27 | +import org.eclipse.jgit.api.Git |
| 28 | +import org.eclipse.jgit.transport.RemoteConfig |
| 29 | +import org.eclipse.jgit.transport.URIish |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * CLI sub-command Push |
| 34 | + * |
| 35 | + * @author Jorge Ejarque <jorge.ejarque@seqera.io> |
| 36 | + */ |
| 37 | +@Slf4j |
| 38 | +@CompileStatic |
| 39 | +@Parameters(commandDescription = "Pushes a local implementation to a remote repository") |
| 40 | +class CmdPush extends CmdBase implements HubOptions { |
| 41 | + |
| 42 | + static final public NAME = 'push' |
| 43 | + |
| 44 | + @Parameter(description = 'Repository URL to push to (optional if already configured as git remote)') |
| 45 | + List<String> args |
| 46 | + |
| 47 | + @Parameter(names=['-d', '-directory'], description = 'Local directory to push (default: current directory)') |
| 48 | + String directory |
| 49 | + |
| 50 | + @Parameter(names=['-r','-revision'], description = 'Revision of the project to run (either a git branch, tag or commit SHA number)') |
| 51 | + String revision = 'main' |
| 52 | + |
| 53 | + @Parameter(names=['-max-size'], description = 'Maximum file size in MB to push without confirmation (default: 10)') |
| 54 | + int maxSizeMB = 10 |
| 55 | + |
| 56 | + @Parameter(names=['-message', '-m'], description = 'Commit message') |
| 57 | + String message = 'Push from nextflow' |
| 58 | + |
| 59 | + @Override |
| 60 | + final String getName() { NAME } |
| 61 | + |
| 62 | + @TestOnly |
| 63 | + protected File root |
| 64 | + |
| 65 | + @Override |
| 66 | + void run() { |
| 67 | + if( args && args.size() > 1){ |
| 68 | + throw new AbortOperationException('Incorrect number of arguments') |
| 69 | + } |
| 70 | + |
| 71 | + // Get repository from args (optional) |
| 72 | + def repository = args && args.size() == 1 ? args[0] : null |
| 73 | + |
| 74 | + // Folder defaults to current working directory if not specified |
| 75 | + def folder = directory |
| 76 | + ? new File(directory).getAbsoluteFile() |
| 77 | + : new File(System.getProperty('user.dir')).getAbsoluteFile() |
| 78 | + |
| 79 | + if( !folder.exists() ) |
| 80 | + throw new AbortOperationException("Folder does not exist: ${folder.absolutePath}") |
| 81 | + |
| 82 | + if( !folder.isDirectory() ) |
| 83 | + throw new AbortOperationException("Path is not a directory: ${folder.absolutePath}") |
| 84 | + |
| 85 | + // init plugin system |
| 86 | + Plugins.init() |
| 87 | + |
| 88 | + try { |
| 89 | + final manager = new PushManager(folder) |
| 90 | + def resolvedRepo = repository |
| 91 | + if( !resolvedRepo ) { |
| 92 | + resolvedRepo = manager.resolveRepository() |
| 93 | + } |
| 94 | + |
| 95 | + log.info "Pushing folder ${folder.absolutePath} to repository ${resolvedRepo}" |
| 96 | + manager.push(resolvedRepo, revision) |
| 97 | + } |
| 98 | + catch( Exception e ) { |
| 99 | + throw new AbortOperationException("Failed to push folder: ${e.message}", e) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments