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