001package co.codewizards.cloudstore.client;
002
003import java.io.File;
004import java.util.UUID;
005
006import org.kohsuke.args4j.Argument;
007
008import co.codewizards.cloudstore.core.repo.local.LocalRepoManager;
009import co.codewizards.cloudstore.core.repo.local.LocalRepoManagerFactory;
010import co.codewizards.cloudstore.core.repo.local.LocalRepoRegistry;
011
012/**
013 * {@link SubCommand} implementation for showing information about a repository in the local file system.
014 *
015 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
016 */
017public class CreateRepoAliasSubCommand extends SubCommandWithExistingLocalRepo
018{
019        @Argument(metaVar="<alias>", index=1, required=true, usage="The alias to be created.")
020        private String alias;
021
022        public CreateRepoAliasSubCommand() { }
023
024        @Override
025        public String getSubCommandDescription() {
026                return "Create an alias for an existing repository.";
027        }
028
029        @Override
030        public void run() throws Exception {
031                LocalRepoRegistry localRepoRegistry = LocalRepoRegistry.getInstance();
032                LocalRepoManager localRepoManager = LocalRepoManagerFactory.Helper.getInstance().createLocalRepoManagerForExistingRepository(localRoot);
033                try {
034                        UUID oldRepositoryId = localRepoRegistry.getRepositoryId(alias);
035
036                        File oldLocalRoot = null;
037                        if (oldRepositoryId != null) {
038                                oldLocalRoot = localRepoRegistry.getLocalRoot(oldRepositoryId);
039                                if (oldLocalRoot == null || !oldLocalRoot.exists()) {
040                                        // orphaned entry to be ignored (should be cleaned up after a while, anyway)
041                                        oldRepositoryId = null;
042                                        oldLocalRoot = null;
043                                }
044                        }
045
046                        if (oldRepositoryId != null)
047                                System.err.println(String.format("ERROR: There is already a repository registered with the alias '%s'! The existing repository's ID is '%s' and its local-root is '%s'.", alias, oldRepositoryId, oldLocalRoot));
048                        else {
049                                localRepoManager.putRepositoryAlias(alias);
050                                System.out.println(String.format("Created alias '%s' for repository %s (local-root '%s').", alias, localRepoManager.getRepositoryId(), localRepoManager.getLocalRoot()));
051                        }
052                } finally {
053                        localRepoManager.close();
054                }
055        }
056}