|
| 1 | +package clap.server.common.utils; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import org.springframework.core.env.Environment; |
| 5 | +import org.springframework.stereotype.Component; |
| 6 | +import org.springframework.util.CollectionUtils; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +@Component |
| 12 | +@RequiredArgsConstructor |
| 13 | +public class SpringEnvironmentHelper { |
| 14 | + |
| 15 | + private final Environment environment; |
| 16 | + |
| 17 | + private final String PROD = "prod"; |
| 18 | + private final String DEV = "dev"; |
| 19 | + private final String LOCAL = "local"; |
| 20 | + |
| 21 | + private final List<String> LOCAL_AND_DEV = List.of("local", "dev"); |
| 22 | + |
| 23 | + public Boolean isProdProfile() { |
| 24 | + String[] activeProfiles = environment.getActiveProfiles(); |
| 25 | + List<String> currentProfile = Arrays.stream(activeProfiles).toList(); |
| 26 | + return currentProfile.contains(PROD); |
| 27 | + } |
| 28 | + |
| 29 | + public Boolean isLocalProfile() { |
| 30 | + String[] activeProfiles = environment.getActiveProfiles(); |
| 31 | + List<String> currentProfile = Arrays.stream(activeProfiles).toList(); |
| 32 | + return currentProfile.contains(LOCAL); |
| 33 | + } |
| 34 | + |
| 35 | + public Boolean isDevProfile() { |
| 36 | + String[] activeProfiles = environment.getActiveProfiles(); |
| 37 | + List<String> currentProfile = Arrays.stream(activeProfiles).toList(); |
| 38 | + return currentProfile.contains(DEV); |
| 39 | + } |
| 40 | + |
| 41 | + public Boolean isLocalAndDevProfile() { |
| 42 | + String[] activeProfiles = environment.getActiveProfiles(); |
| 43 | + List<String> currentProfile = Arrays.stream(activeProfiles).toList(); |
| 44 | + return CollectionUtils.containsAny(LOCAL_AND_DEV, currentProfile); |
| 45 | + } |
| 46 | +} |
0 commit comments